检查字符串是否匹配所有字符,但是匹配另一个字符串

时间:2012-10-14 15:26:53

标签: java string list loops split

我有一个字符串列表,每个字符串我都要检查它的字符是否与其他字符串相比,以查看除了一个字符串之外是否所有字符都相同。

例如,将返回true的检查将是检查

反对锁定

时钟和鸡群有一个不同的角色,不多也不少。

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

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

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

无论如何我有很多字符串(4029)并且考虑到我现在想要实现的内容将包含3个循环,每个循环都会导致一个立方循环(?),这需要很长时间很多元素不会吗?

有更简单的方法吗?或者这种方法真的可行吗?或者 - 但是 - 但是在我提出的解决方案中是否存在某种潜在的逻辑缺陷?

非常感谢!

5 个答案:

答案 0 :(得分: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;
}

现在你可以使用二次算法来检查每个字符串。

答案 1 :(得分: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");
            }
        }

答案 2 :(得分:0)

不确定这是否是最好的方法,但即使两个字符串的长度不同,这个方法也能正常工作。例如:cat&amp; cattp它们相差一个字符p和t重复。看起来像O(n)时间解决方案使用额外的空间用于hashmap&amp;字符数组。

/**
 * 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;
}

答案 3 :(得分: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");
              }
        }

答案 4 :(得分: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;
}