2012-10-14 105 views
3

我有一个字符串列表和每个字符串,我想检查它的字符是否与其他字符串相同,以查看它的所有字符是否相同。检查一个字符串是否与另一个字符串中的所有字符匹配

例如检查都将返回true将检查

岩石对锁

时钟和羊群有一个字符是不同的,不多不少。

岩石对凹痕显然会返回假。

我一直在考虑首先循环遍历列表,然后在该列表中检查第一个字符串与第二个循环。

然后使用split("");创建两个数组,包含每个字符串的字符,然后检查数组元素对方(即比较每个字符串与另一个数组中的相同位置1-1 2-2等...) ),只要只有一个字符比较失败,那么检查这两个字符串是否为真。

无论如何,我有很多字符串(4029),并考虑我目前正在考虑实现的内容中将包含3个循环,每个循环内会导致一个需要很长时间的三次循环(?)有那么多元素不是吗?

有没有更简单的方法来做到这一点?或者这种方法实际上可行吗?或者 - 没有 - 但是在我提出的解决方案中是否存在某种潜在的逻辑缺陷?

非常感谢!

回答

5

为什么不做这种天真的方式?

bool matchesAlmost(String str1, String str2) { 
    if (str1.length != str2.length) 
     return false; 
    int same = 0; 
    for (int i = 0; i < str1.length; ++i) { 
     if (str1.charAt(i) == str2.charAt(i)) 
      same++; 
    } 
    return same == str1.length - 1; 
} 

现在,您可以使用二次算法来检查每一个字符串与其他字符串。

+0

干杯,我很喜欢这种方式,很多:) – DanMc

0

假设两个字符串的长度相等

String str1 = "rock"; 
     String str2 = "lick"; 

     if(str1.length() != str2.length()) 
      System.out.println("failed"); 

     else{ 
      if(str2.contains(str1.substring(0, str1.length()-1)) || str2.contains( str1.substring(1, str1.length()))){ 
       System.out.println("Success "); 
      } 

      else{ 
       System.out.println("Failed"); 
      } 
     } 
+0

什么'rock'和' rick'? –

+0

哎呀!我没有拿这个测试用例。上面的一个很好!非常遗憾! – madhairsilence

0

不知道这是最好的方法,但是当两个字符串长度相同的不是这一个工程连。例如:cat & cattp它们相差一个字符p并重复t。看起来像O(n)时间解决方案使用额外的空间散列图&字符数组。

/** 
* Returns true if two strings differ by one character 
* @param s1 input string1 
* @param s2 input string2 
* @return true if strings differ by one character 
*/ 
boolean checkIfTwoStringDifferByOne(String s1, String s2) { 
    char[] c1, c2; 

    if(s1.length() < s2.length()){ 
     c1 = s1.toCharArray(); 
     c2 = s2.toCharArray(); 
    }else{ 
     c1 = s2.toCharArray(); 
     c2 = s1.toCharArray(); 
    } 

    HashSet<Character> hs = new HashSet<Character>(); 

    for (int i = 0; i < c1.length; i++) { 
     hs.add(c1[i]); 
    } 
    int count = 0; 
    for (int j = 0; j < c2.length; j++) { 
     if (! hs.contains(c2[j])) { 
      count = count +1; 
     } 
    } 

    if(count == 1) 
     return true; 
    return false; 
} 
0
Best way is to concatenate strings together one forward and other one in reverse order. Then check in single loop for both ends matching chars and also start from middle towards ends matching char. If more than 2 chars mismatch break. 
If one mismatch stop and wait for the next one to complete if it reaches the same position then it matches otherwise just return false. 

    public static void main(String[] args) { 

     // TODO code application logic here 
     New1 x = new New1(); 
     x.setFunc(); 
     } 

     static void setFunc(){ 
      Set s   = new HashSet<Character>(); 
      String input = " aecd"; 
      String input2 = "abcd"; 
      String input3 = new StringBuilder(input2).reverse().toString(); 
      String input4 = input.concat(input3); 
      int length = input4.length(); 

      System.out.println(input4); 
      int flag  = 0; 

      for(int i=1,j=length-1;j>i-1; i++,j--){ 

      if(input4.charAt(i)!=input4.charAt(j)){ 

       System.out.println(input4.charAt(i)+" doesnt match with "+input4.charAt(j)); 
        if(input4.charAt(i+1)!=input4.charAt(j)){ 
         System.out.println(input4.charAt(i+1)+" doesnt match with "+input4.charAt(j)); 
         flag = 1; 
         continue; 
        } else if(input4.charAt(i)!=input4.charAt(j-1)){ 
         System.out.println(input4.charAt(i)+" doesnt match with "+input4.charAt(j-1)); 
         flag = 1; 
         break; 
        } else if(input4.charAt(i+1)!=input4.charAt(j-1) && i+1 <= j-1){ 
         System.out.println(input4.charAt(i+1)+" doesnt match with xxx "+input4.charAt(j-1)); 
         flag = 1; 
         break; 
        } 
        } else { 
         continue; 
        } 
      } 

       if(flag==0){ 
        System.out.println("Strings differ by one place"); 
       } else { 
        System.out.println("Strings does not match"); 
       } 
     } 
0

假设所有字符串的长度相同,我认为这将有助于:

public boolean differByOne(String source, String destination) 
{ 
    int difference = 0; 

    for(int i=0;i<source.length();i++) 
    { 
     if(source.charAt(i)!=destination.charAt(i)) 
     { 
      difference++; 

      if(difference>1) 
      { 
       return false; 
      } 
     } 
    } 

    return difference == 1; 
} 
相关问题