Rotate String
题目
Given two words, find if second word is the round rotation of first word.
For example: abc, cab
return 1
since cab is round rotation of abc
Example2: ab, aa
return -1
since ab is not round rotation for aa
Idea: 如果是经过翻转所得,那么先连接word1 + word1,然后判断是否包含word2即可。
import java.lang.String;
public class RorateString {
public static int isRotation(String word1, String word2) {
if (word1 == null || word2 == null || word1.length() == 0 || word2.length() == 0 || word1.length() != word2.length()) {
return -1;
}
String str = word1 + word1;
return str.indexOf(word2) != -1 ? 1 : -1;
}
}