, ( ) , , .
, , : "ABAB" "ABABAB" "ABAB", 0 "ABAB", 2. , wIndex = startIndex.
:
public static void main (String[] args) throws java.lang.Exception {
String word = "B BABBABBA B";
String seq = "ABBA";
char[] wChars = word.toCharArray();
char[] sChars = seq.toCharArray();
int wIndex = 0;
int sIndex = 0;
int startIndex = 0;
System.out.println("Starting to evaluate...");
while(wIndex < wChars.length) {
if(wChars[wIndex] == sChars[sIndex]) {
if(sIndex == 0) {
startIndex = wIndex;
}
sIndex += 1;
} else {
sIndex = 0;
}
if(sIndex >= sChars.length) {
System.out.println("Sequence \"" + seq + "\" found at index " + startIndex + ".");
sIndex = 0;
wIndex = startIndex;
}
wIndex += 1;
}
}
:
Starting to evaluate...
Sequence "ABBA" found at index 3.
Sequence "ABBA" found at index 6.