题解 | #找到字符串中的异位词#

找到字符串中的异位词

http://www.nowcoder.com/practice/9ff491c910e5427fab6ae67745929085

import java.util.*;

public class Solution {

//先对p字符串进行排序,生成新的字符串,然后判断s的子字符串是否包含有新的p字符串
public ArrayList<Integer> findWord (String s, String p) {
    // write code here
    int pLength = p.length();
    int sLength = s.length();
    if(sLength<pLength){
        return null;
    }
    ArrayList<Integer> resultArrayList = new ArrayList<>();
    char[] pCharArray = p.toCharArray();
    String tempString;
    Arrays.sort(pCharArray);
    p = new String(pCharArray);
    for (int i = 0; i < sLength-pLength+1; i++) {
            tempString=s.substring(i,i+pLength);
            if(judge(tempString,p)){
                resultArrayList.add(i);
            }
    }
    return resultArrayList;
}

private boolean judge(String tempString, String p) {
    char[] charArray = tempString.toCharArray();
    Arrays.sort(charArray);
    tempString=new String(charArray);
    return tempString.contains(p);
}

}

全部评论

相关推荐

01-26 19:51
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务