-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringRotation.java
More file actions
35 lines (33 loc) · 934 Bytes
/
stringRotation.java
File metadata and controls
35 lines (33 loc) · 934 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
30
31
32
33
34
35
public class stringRotation {
public static boolean isSubtring(String a, String b) {
int p1 = 0, p2 = 0;
while (p1 < a.length()) {
if (p2 == b.length()) {
return true;
}
if (a.charAt(p1) == b.charAt(p2)) {
p1 += 1;
p2 += 1;
} else {
p2 = 0;
p1 += 1;
}
}
return false;
}
/*
* Break string a into two parts, x and y. If b was a rotation of a there should
* be some yx s.t. b = yx and a = xy. a + a = xyxy and note that yx appears in
* here
* Thus, if b is a rotation of a, b should appear as a substring of aa
*
*
*
*/
public static boolean stringRotationFunc(String a, String b) {
if (a.length() != b.length()) {
return false;
}
return isSubtring(a + a, b);
}
}