https://github.com/careercup/CtCI-6th-Edition/blob/master/Java/Ch%2001.%20Arrays%20and%20Strings/Q1_09_String_Rotation/Question.java
public static boolean isSubstring(String big, String small) {
if (big.indexOf(small) >= 0) {
return true;
} else {
return false;
}
}
public static boolean isRotation(String s1, String s2) {
int len = s1.length();
/* check that s1 and s2 are equal length and not empty */
if (len == s2.length() && len > 0) {
/* concatenate s1 and s1 within new buffer */
String s1s1 = s1 + s1;
return isSubstring(s1s1, s2);
}
return false;
}
Check if one string is Rotation of another string | Algorithms
Write an algorithm to check if one string is Rotation of another string.
https://github.com/jingz8804/Algorithms-and-Problems/blob/master/src/careerCup/IsRotation.java
private static boolean isRotation(String str1, String str2){
if(str1 == null or str2 == null) return false;
if (str1.length == str2.length && (str1 + str1).indexOf(str2) > 0) return true;
return false;
}
算法复杂度是O(n^2)
public boolean isRotation(String s1, String s2){
// check same character set
int freq[] = new int[26];
for(int i = 0;i < s1.length();i++)
freq[(int)(s1.charAt(i)-'a')]++;
for(int i = 0;i < s2.length();i++)
freq[(int)(s2.charAt(i)-'a')]--;
for(int i = 0;i < freq.length;i++)
if(freq[i]!=0) return false;
// check rotation
for(int i = 0;i < s1.length();i++)
if(s1.charAt(i)==s2.charAt(0))
if(isEnd(s1, i, s2)) return isSubstring(s1.substring(0,i),s2);
return false;
}
private boolean isEnd(String s1, int index, String s2){
for(int i = index,j = 0;i < s1.length();i++,j++)
if(s1.charAt(i)!=s2.charAt(j)) return false;
return true;
}
public boolean isSubstring(String s1, String s2){
return s2.indexOf(s1) >= 0;
}
Read full article from Check if one string is Rotation of another string | Algorithms
Assume you have a method i 5Su b 5 tr ing which checks if one word is a substring of another. Given two strings, 51 and 52, write code to check if 52 is a rotation of 51 using only one call to isSubString (e.g., "waterbottle" is a rotation of" erbottlewat").
public static boolean isSubstring(String big, String small) {
if (big.indexOf(small) >= 0) {
return true;
} else {
return false;
}
}
public static boolean isRotation(String s1, String s2) {
int len = s1.length();
/* check that s1 and s2 are equal length and not empty */
if (len == s2.length() && len > 0) {
/* concatenate s1 and s1 within new buffer */
String s1s1 = s1 + s1;
return isSubstring(s1s1, s2);
}
return false;
}
Write an algorithm to check if one string is Rotation of another string.
https://github.com/jingz8804/Algorithms-and-Problems/blob/master/src/careerCup/IsRotation.java
private static boolean isRotation(String str1, String str2){
if(str1 == null or str2 == null) return false;
if (str1.length == str2.length && (str1 + str1).indexOf(str2) > 0) return true;
return false;
}
public static boolean checkRotation(String s1, String s2){
if(s2.length()!=s1.length()) return false;
String s1s1 = s1+s1;
return isSubstring(s1s1, s2);
}
public static boolean isSubstring(String original, String beingChecked){
int sLength = original.length();
int tLength = beingChecked.length();
int j=0, i = 0;
boolean found = false;
for(; j < tLength && i <= sLength - tLength;++i){
int i2 = i;
while(original.charAt(i2) == beingChecked.charAt(j)){
i2++;j++;
if(i2==sLength || j==tLength){found = true; break;}
}
if(found)break; else j = 0;
}
return found;
}
http://siyang2notleetcode.blogspot.com/2015/02/check-rotation-using-one-issubstring.html算法复杂度是O(n^2)
public boolean isRotation(String s1, String s2){
// check same character set
int freq[] = new int[26];
for(int i = 0;i < s1.length();i++)
freq[(int)(s1.charAt(i)-'a')]++;
for(int i = 0;i < s2.length();i++)
freq[(int)(s2.charAt(i)-'a')]--;
for(int i = 0;i < freq.length;i++)
if(freq[i]!=0) return false;
// check rotation
for(int i = 0;i < s1.length();i++)
if(s1.charAt(i)==s2.charAt(0))
if(isEnd(s1, i, s2)) return isSubstring(s1.substring(0,i),s2);
return false;
}
private boolean isEnd(String s1, int index, String s2){
for(int i = index,j = 0;i < s1.length();i++,j++)
if(s1.charAt(i)!=s2.charAt(j)) return false;
return true;
}
public boolean isSubstring(String s1, String s2){
return s2.indexOf(s1) >= 0;
}
Read full article from Check if one string is Rotation of another string | Algorithms