我需要在两个字符串中获得不匹配字符的计数。例如
string 1 "hari", string 2 "malar"
现在我需要从字符串['a'和&中删除重复项。 'r']在两个字符串中都很常见,所以删除它,现在字符串1包含“hi”字符串2包含“mla”。
剩余 count = 5
我尝试了这段代码,如果重复/重复不能在同一个sting中使用,它的工作正常,就像这里'a'在 string 2 中来了两次,所以我的代码无法正常工作。
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;
首先是&amp;第二个是指
char[] first = nameone.toCharArray();
char[] second = nametwo.toCharArray();
此代码适用于String 1 "sri" string 2 "hari"
这里的字符串字符没有重复,所以上面的代码工作正常。帮帮我解决这个问题?
答案 0 :(得分: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());
}
希望这是你需要的。如果不是我会更新我的答案
答案 1 :(得分: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功能,这会减少它(但不会像我希望的那样多)。
然而,它会产生有意义的中间结果。但它还要长得多: - (
答案 2 :(得分: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;
答案 3 :(得分: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
答案 4 :(得分:1)
你在这里缺少两件事。
在下面的代码中进行了这两项更改,现在它按预期打印结果。
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);
答案 5 :(得分:-1)
如果字符匹配,您只需要遍历两个字符串,增加计数,然后从两个字符的总长度中删除这些计数
s = 'hackerhappy'\
t = 'hackerrank'\
count = 0
for i in range(len(s)):
for j in range(len(t)):
if s[i] == t[j]:
count += 2
break
char_unmatched = (len(s)+len(t)) - count
char_unmatched 包含两个字符串中不相等的字符数