2014-09-20 42 views
1

我需要獲得無與倫比的字符數在兩個字符串。例如如何獲得兩個字符串中不匹配字符的數量?

string 1 "hari", string 2 "malar" 

現在我需要從兩個字符串[「A」 &「R」]刪除重複在兩個字符串共同以便移除,現在串1包含「喜」串2含有「MLA 「

剩餘數= 5

我嘗試這個代碼,它的工作很好,如果重複/ repeart是相同蜇喜歡這裏不提供字符串「一」來兩次2所以我的代碼沒」 t正常工作。

for (int i = 0; i < first.length; i++) { 
       for (int j = 0; j < second.length; j++) { 

        if(first[i] == second[j]) 
        { 
         getstrings = new ArrayList<String>(); 
         count=count+1; 
         Log.d("Matches", "string char that matched "+ first[i] +"==" + second[j]);       
        } 
       } 
      } 
      int tot=(first.length + second.length) - count; 

這裏第一&二是指

char[] first = nameone.toCharArray(); 
char[] second = nametwo.toCharArray(); 

這個代碼在一個字符串的字符爲String 1 "sri" string 2 "hari"做工精細這裏沒有重複所以這上面的代碼是工作的罰款。幫我解決這個問題?

回答

2

這裏是我的解決方案,

public static void RemoveMatchedCharsInnStrings(String first,String second) 
    { 
     for(int i = 0 ;i < first.length() ; i ++) 
     { 
      char c = first.charAt(i); 
      if(second.indexOf(c)!= -1) 
      { 
       first = first.replaceAll(""+c, ""); 
       second = second.replaceAll(""+c, ""); 
      } 
     } 
     System.out.println(first); 
     System.out.println(second); 
     System.out.println(first.length() + second.length()); 

    } 

希望這是你所需要的。如果不是,我會更新我的答案

+0

謝謝你的回答這段代碼工作正常,如果字符didn'在我的情況下,字符可能會重複,就像我上面說的'hari'' malar'這個代碼從string1中取一個字符串並檢查string2,如果char是yes從字符串中刪除所有相同的字符串,結果是'hi'' ml'。 – Sri 2014-09-21 07:43:58

+0

in string 1'hari'只有一個字符可用'a',所以我只需要替換字符串2'malar'中的一個字符,期望的輸出是'hi'' mla',因此總計數是5。在此代碼中更改'second.replaceAll(「」+ c,「」);' – Sri 2014-09-21 07:47:35

+0

在此代碼中進行了一些更改,我解決了非常感謝您的回答和您的時間 – Sri 2014-09-21 09:43:42

1

試試這個代碼:

String first = "hari"; 
String second = malar; 

String tempFirst = ""; 
String tempSecond = ""; 

int maxSize = ((first.length() > second.length()) ? (first.length()) : (second.length())); 

for (int i = 0; i < maxSize; i++) { 
    if (i >= second.length()) { 
     tempFirst += first.charAt(i); 
    } else if (i >= first.length()) { 
     tempSecond += second.charAt(i); 
    } else if (first.charAt(i) != second.charAt(i)) { 
     tempFirst += first.charAt(i); 
     tempSecond += second.charAt(i); 
    } 
} 

first = tempFirst; 
second = tempSecond; 
+0

yaa解決了謝謝你的回答和寶貴的時間 – Sri 2014-09-21 09:38:48

1

你一旦需要break;如找到匹配:

public static void main(String[] args) { 
     String nameone="hari"; 
     String nametwo="malar"; 
     char[] first = nameone.toCharArray(); 
     char[] second = nametwo.toCharArray(); 
     List<String>getstrings=null; 
     int count=0; 
     for (int i = 0; i < first.length; i++) { 
      for (int j = 0; j < second.length; j++) { 

       if(first[i] == second[j]) 
       { 
        getstrings = new ArrayList<String>(); 
        count++; 
        System.out.println("Matches"+ "string char that matched "+ first[i] +"==" + second[j]); 
        break; 
       } 
      } 
     } 
     //System.out.println(count); 
     int tot=(first.length-count)+ (second.length - count); 

     System.out.println("Remaining after match from both strings:"+tot); 

} 

打印:

Remaining after match from both strings:5 
+0

yaa解決了謝謝你的回答和寶貴的時間 – Sri 2014-09-21 09:40:43

1

你在這裏失蹤的兩件事。

  1. 在if條件中,當兩個字符匹配時,您需要將計數遞增2,而不是一個,因爲您從兩個字符串中刪除。
  2. 由於您始終與首次出現的角色相匹配,因此您需要在入境條件中休息一下。

在您的代碼中進行了如下兩項更改,現在它按預期打印結果。

for (int i = 0; i < first.length; i++) { 
      for (int j = 0; j < second.length; j++) { 

       if(first[i] == second[j]) 
       {      
        count=count+2; 
        break; 
       } 
      } 
     } 
     int tot=(first.length + second.length) - count; 
     System.out.println("Result = "+tot); 
+0

yaa解決了謝謝你的答案。 – Sri 2014-09-21 09:39:59

2

我看到了其他的答案和想法:必須有更多的聲明和可組合的方式來做到這一點! 有,但它是更長的時間......

public static void main(String[] args) { 
    String first = "hari"; 
    String second = "malar"; 
    Map<Character, Integer> differences = absoluteDifference(characterCountOf(first), characterCountOf(second)); 
    System.out.println(sumOfCounts(differences)); 
} 

public static Map<Character, Integer> characterCountOf(String text) { 
    Map<Character, Integer> result = new HashMap<Character, Integer>(); 
    for (int i=0; i < text.length(); i++) { 
     Character c = text.charAt(i); 
     result.put(c, result.containsKey(c) ? result.get(c) + 1 : 1); 
    } 
    return result; 
} 

public static <K> Set<K> commonKeys(Map<K, ?> first, Map<K, ?> second) { 
    Set<K> result = new HashSet<K>(first.keySet()); 
    result.addAll(second.keySet()); 
    return result; 
} 

public static <K> Map<K, Integer> absoluteDifference(Map<K, Integer> first, Map<K, Integer> second) { 
    Map<K, Integer> result = new HashMap<K, Integer>(); 
    for (K key: commonKeys(first, second)) { 
     Integer firstCount = first.containsKey(key) ? first.get(key) : 0; 
     Integer secondCount = second.containsKey(key) ? second.get(key) : 0; 
     Integer resultCount = Math.max(firstCount, secondCount) - Math.min(firstCount, secondCount); 
     if (resultCount > 0) result.put(key, resultCount); 
    } 
    return result; 
} 

public static Integer sumOfCounts(Map<?, Integer> map) { 
    Integer sum = 0; 
    for (Integer count: map.values()) { 
     sum += count; 
    } 
    return sum; 
} 

這是我比較喜歡的解決方案 - 但它的很多時間。你已經用Android標記了這個問題,所以我沒有使用任何Java 8特性,這會減少一些(但不像我希望的那樣)。

但是它會產生有意義的中間結果。但它還是這麼長:-(

相關問題