671

来源:互联网 发布:夏易网络官网 编辑:程序博客网 时间:2024/05/12 19:16

2017.9.9

判断两个字符串是不是循环单词。

public static boolean isSame(String str1, String str2){
    if(str1.length() != str2.length()){
        return false;
    }
    str1 = str1.concat(str1);
    if(str1.contains(str2)){
        return true;
    }
    else{
        return false;
    }
}

先将str1 两遍加起来。

在判断新的str1 是不是contain str2。

刚开始理解错了,以为只要是字母的组合一致就可以,其实还要考虑是不是循环。

public class Solution {    /*     * @param words: A list of words     * @return: Return how many different rotate words     */   public static int countRotateWords(List<String> words) {        // Write your code hereif(words.isEmpty()){return 0;}int count = words.size();Object []str = words.toArray();boolean []flag = new boolean[count];for(int i = 0; i< count - 1; i++){if(flag[i] == true){continue;}String str1 = str[i].toString();for(int j = i+1; j < count; j++){String str2 = str[j].toString();if(isSame(str1,str2) == true){flag[j] = true;}}}for(int i = 0;i< words.size(); i++){if(flag[i] == true){count--;}}return count;}public static boolean isSame(String str1, String str2){if(str1.length() != str2.length()){return false;}str1 = str1.concat(str1);if(str1.contains(str2)){return true;}else{return false;}}}