关注
滑动窗口+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;
}
查看原帖
点赞 评论
相关推荐
牛客热帖
更多
正在热议
更多
# 在大厂上班是一种什么样的体验 #
10718次浏览 134人参与
# 你认为工作的意义是什么 #
249240次浏览 1498人参与
# 程序员找工作至少要刷多少题? #
18492次浏览 248人参与
# 为了减少AI幻觉,你注入过哪些设定? #
4626次浏览 150人参与
# 我现在比当时_,你想录用我吗 #
8680次浏览 111人参与
# 机械人避雷的岗位/公司 #
43444次浏览 300人参与
# 一张图晒一下你的AI员工 #
5063次浏览 114人参与
# 论秋招对个人心气的改变 #
10893次浏览 155人参与
# 关于春招/暑期实习,你想知道哪些信息? #
7482次浏览 119人参与
# 刚入职的你踩过哪些坑 #
6863次浏览 127人参与
# AI Coding的使用心得 #
4656次浏览 101人参与
# 晒晒你司的新年福利 #
8455次浏览 105人参与
# 牛客AI体验站 #
6770次浏览 187人参与
# 12306一秒售罄,你抢到回家的票了吗? #
1963次浏览 47人参与
# 柠檬微趣工作体验 #
14777次浏览 83人参与
# 总结:哪家公司面试体验感最差 #
93018次浏览 430人参与
# 程序员能干到多少岁? #
8627次浏览 115人参与
# 你认为小厂实习有用吗? #
118056次浏览 679人参与
# 互联网公司评价 #
485625次浏览 4109人参与
# 应届生进小公司有什么影响吗 #
118291次浏览 1159人参与