public class Solution {
public static void main(String
[] args
) {
Solution solution
= new Solution();
int a
= solution
.ans("helolo","ll");
System
.out
.println(a
);
}
public int ans(String s
, String t
) {
if (!s
.contains(t
)) return -1;
int temp
= 0, i
= 0, j
= 0;
char[] arrS
= s
.toCharArray();
char[] arrT
= t
.toCharArray();
while (i
< arrS
.length
&& j
< arrT
.length
) {
if (arrS
[i
] == arrT
[j
]) {
temp
+= 1;
i
++;
j
++;
} else {
temp
= 0;
i
++;
j
= 0;
}
if (temp
== arrT
.length
) return i
- arrT
.length
;
}
return -1;
}
}
转载请注明原文地址: https://lol.8miu.com/read-15200.html