-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanABeShiftedToBecomeB.java
63 lines (54 loc) · 1.77 KB
/
CanABeShiftedToBecomeB.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.ArrayList;
import java.util.List;
/*
Given two strings A and B, return whether or not A can be shifted some number of times to get B.
For example, if A is abcde and B is cdeab, return true. If A is abc and B is acb, return false.
*/
public class CanABeShiftedToBecomeB {
public static void main(String[] args) {
String a = "abcde";
String b = "cdeab";
System.out.println("Can A be shifted to become B? : " + canAbeShiftedToGetB(a, b));
}
private static boolean canAbeShiftedToGetB(String a, String b) {
if(a == null || b ==null){
return false;
}
if(a.length() != b.length()){
return false;
}
char[] arrA = a.toCharArray();
char[] arrB = b.toCharArray();
List<Integer> firstCharIndexes = getCharIndexes(arrA[0], b);
if(firstCharIndexes.isEmpty()){
return false;
}
int lastIndex = arrA.length - 1;
for(int indexInB : firstCharIndexes) {
boolean matchFound = true;
for(int i = 0; i < arrA.length -1; i++){
if(arrA[i] != arrB[indexInB]) {
matchFound = false;
break;
}
indexInB++;
if(indexInB > lastIndex){
indexInB = 0;
}
}
if(matchFound){
return true;
}
}
return false;
}
private static List<Integer> getCharIndexes(char c, String word) {
List<Integer> charIndexes = new ArrayList();
int index = word.indexOf(c, 0);
while (index >= 0){
charIndexes.add(index);
index = word.indexOf(c,index + 1);
}
return charIndexes;
}
}