-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPatternMatching2.java
More file actions
29 lines (26 loc) · 864 Bytes
/
PatternMatching2.java
File metadata and controls
29 lines (26 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package strings;
public class PatternMatching2 {
public static void main(String[] args) {
String str = "ABCWABCDABDABC";
String pattern = "ABCD";
int n = str.length();
int p = pattern.length();
for (int i = 0; i <= (n - p);) {
int j;
for (j = 0; j < p; j++) {
if (pattern.charAt(j) != str.charAt(i + j)) {
break; // if pattern not match then exist
}
} // jth loop ends
if (j == p) {
System.out.println("Pattern Found " + i);
// return ; // for first found
}
if (j == 0) {
i++; // str move to the next character
} else {
i = i + j; // Jump to the jth index from where pattern not match
}
}
}
}