Open
Description
string S = "abcdefx";
string T = "def";
int myIndex(string S,string T,int pos) {
int i = pos;
int j = 0;
byte[] s = toByteArray(S);
byte[] t = toByteArray(T);
int slen = len(s);
int tlen = len(t);
while(i <= slen && j <= tlen) { //undefined: while
// for(i <= slen && j <= tlen) {
if (s[i] == s[j]) {
i += 1;
j += 1;
}else{
i = i - j + 1;
j = 0;
}
}
if (j == tlen) {
return i - tlen;
} else {
return 0;
}
}
int res = myIndex(S,T,0);
println(res);