671 - 循环单词

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 here
		if(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;
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值