-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
StringRotation.java
39 lines (35 loc) · 1.29 KB
/
StringRotation.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
package com.ctci.arraysandstrings;
/**
* Assume you have a method isSubString which checks if one word is a substring of another. Given two
* strings, S1 and S2, write code to check if S2 is a rotation of S1 using only one call to isSubString
* (e.g., "waterbottle" is a rotation of" erbottlewat").
*
* @author rampatra
* @since 2019-01-22
*/
public class StringRotation {
private static boolean isStringRotation(String s1, String s2) {
if (s1.length() != s2.length()) {
return false;
}
s2 = s2 + s2;
return isSubString(s1, s2);
}
/**
* Given method in question.
*
* @param s1 first string
* @param s2 second string
* @return {@code true} if s1 is a substring of s2 or else {@code false}
*/
private static boolean isSubString(String s1, String s2) {
return s2.contains(s1);
}
public static void main(String[] args) {
System.out.println(isStringRotation("waterbottle", "erbottlewat")); // true
System.out.println(isStringRotation("rampatra", "atraramp")); // true
System.out.println(isStringRotation("rampatra", "arampata")); // false
System.out.println(isStringRotation("rampatra", "arampat")); // false
System.out.println(isStringRotation("", "")); // true
}
}