滑动窗口+Hash吧, O(n) 的复杂度,比较时只需要对比两个串hash之后的值。 public static int find(String father, String child){ int len1 = father.length(); int len2 = child.length(); if(len1<len2 || len1<=0){ return -1; } // 初始化2的N次方 int[] N2 = new int[26]; for(int i=0; i<26; i++){ N2[i] = (int) Math.pow(2,i); } // 计算child的值 int childValue = 0; for(int i=0; i<len2; i++){ childValue += N2[child.charAt(i)-'a']; } // System.out.println("childValue---"+ childValue); int fatherValue = 0; for(int i=0; i<len1; i++){ if(i>=len2){ fatherValue -= N2[father.charAt(i-len2)-'a']; } fatherValue += N2[father.charAt(i)-'a']; if(fatherValue == childValue){ return i-len2+1; } } return -1; }
点赞 评论

相关推荐

allin秋招的大菠萝很爱交友:后续,已拿offer ~查看图片
点赞 评论 收藏
分享
牛客网
牛客企业服务