如何计算两个字符串中的所有相同字符

时间:2017-01-14 10:10:36

标签: java

这里有两个字符串的问题,例如:

String name1 = Joshuaa
String name2 = Hazel

我的输出应为= 5

因为h = 2;一个= 3

OR

String name1 = aaabbcd
String name2 = aabbbefg

输出= 10;因为a = 5且b = 5

到目前为止,这是我的代码,但我的输出错误

name1 = name1.replaceAll("\\s","").toUpperCase();
name2 = name2.replaceAll("\\s","").toUpperCase();

nl1 = name1.length();
nl2 = name2.length();

char[] arrn1 = name1.toCharArray();
char[] arrn2 = name2.toCharArray();

for(i=0;i<nl1;i++)
{
  for(j=0;j<nl2;j++)
  {
     if(arrn1[i]==arrn2[j])
     {
         count++;
     }

  }
}

3 个答案:

答案 0 :(得分:2)

如果你使用java 8,你可以这样做:

String name1 = "Joshuaa";
String name2 = "Hazel";

name1 = name1.toLowerCase();
name2 = name2.toLowerCase();

// create maps "character -> amount of this character in the word"
Map<Integer, Integer> map1 = name1.chars()
        .boxed()
        .collect(Collectors.groupingBy(Function.identity(),Collectors.summingInt(x -> 1)));

Map<Integer, Integer> map2 = name2.chars()
        .boxed()
        .collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(x -> 1)));

// take characters from first word containing in the second word and then sum their counts
int count = map1.entrySet().stream()
        .filter(x -> map2.containsKey(x.getKey()))
        .mapToInt(x -> x.getValue() + map2.get(x.getKey()))
        .sum();

System.out.println(count);

答案 1 :(得分:1)

您的代码中的问题是,当您有匹配时,如果它是第一场比赛,您应该将计数增加2,如果不是,则增加1。此外,你应该跟踪你有哪些字母,如果你已经遇到过当前的字符,那么这个字母应该只增加1。

    String name = "Hazel";
    String name2 = "Joshuaa";
    Set<Character> lettersSoFar = new HashSet<>();

    char[] chars = name.toLowerCase(Locale.US).toCharArray();
    char[] chars2 = name2.toLowerCase(Locale.US).toCharArray();

    int count = 0;
    for (char firstCh : chars) {
        boolean isFirstOccurrence = !lettersSoFar.contains(firstCh);
        for (char secondCh : chars2) {
            if (firstCh == secondCh) {
                if (isFirstOccurrence) {
                    count += 2;
                    isFirstOccurrence = false;
                    lettersSoFar.add(firstCh);
                } else {
                    count++;
                }
            }
        }
    }

更新时没有设置

    String name2 = "Hazel";
    String name = "Joshuaa";
    char[] lettersSoFar = new char[name.length() + name2.length()];

    char[] chars = name.toLowerCase(Locale.US).toCharArray();
    char[] chars2 = name2.toLowerCase(Locale.US).toCharArray();

    int count = 0;
    int lettersSoFarCount = 0;
    for (char firstCh : chars) {
        boolean isFirstOccurrence = true;
        for (char letter : lettersSoFar){
            if (letter == firstCh) {
                isFirstOccurrence = false;
                break;
            }
        }

        for (char secondCh : chars2) {
            if (firstCh == secondCh) {
                if (isFirstOccurrence) {
                    count += 2;
                    isFirstOccurrence = false;
                    lettersSoFar[lettersSoFarCount++] = firstCh;
                } else {
                    count++;
                }
            }
        }

    }

答案 2 :(得分:0)

以下是您正在寻找的解决方案: 你问你的输出应该是5,因为它计算2个字符串中相似的所有值,总计发生的次数。

public class NewClass {
        public static void main(String[] args){
// String name1 = "Hazel"; 
//         String name2 ="Joshuaa";
String name1="Lezah";
String name2="Aauhsoj";


         int n1,n2,count=0;

         name1=name1.toUpperCase();
         name2=name2.toUpperCase();
         n1=name1.length();
         n2=name2.length();

         char[] c1=new char[n1];
         for(int i=0;i<n1;i++){
         c1[i]=name1.charAt(i);
         }

         char[] c2=new char[n2];
         for(int i=0;i<n2;i++){
         c2[i]=name2.charAt(i);
         }

         for(int i=0;i<n1;i++){
         for(int j=0;j<n2;j++){
         if(c1[i]==c2[j]){
            //checking if the character is already occur,then count-1
             for(int l=i-1;l>0||l==0;l--){
             if(c1[i]==c1[l]){
             count-=1;
             }
             }
             for(int u=j-1;u>0||u==0;u--){
             if(c2[j]==c2[u]){
             count-=1;
             }
             }
         //if characters are same add 2 in count
         count+=2;
         }
         }
         }


            System.out.println(count);

        }
}