2014-09-20 72 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特性,这会减少一些(但不像我希望的那样)。

但是它会产生有意义的中间结果。但它还是这么长:-(

相关问题