2012-10-14 77 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; 
} 
相關問題