Iterate Over 2 Maps in Java

MooDoesCow :

Doing commonCharacterCount from CodeSignal. Want to see how many Character are in common, of course.

Here's an example:

For s1 = "aabcc" and s2 = "adcaa", the output should be commonCharacterCount(s1, s2) = 3.

strings have 3 common characters - 2 "a"s and 1 "c".

Thougth about building an map with char count from each. Then I want to check every value in a map, and check both of them. To the answer I will add the minimum count of the char in both Maps. For instance, s1 = "aabcc" && s2 = "adcaa", mapS1['a'] = 2 && mapS2['a'] = 3, so count += min(2,3) = 2

That's my idea, but having some trouble with iterating both Maps as I mentioned.

First time using a map's value as a counting value. Can't really tell if that's the best way to increment a value in a map, so I guess I will need help with that as well.

Thank you in advance :)

static int res(String s1, String s2) {
                HashMap<Character, Integer> mapS1 = new HashMap<>();
                HashMap<Character, Integer> mapS2 = new HashMap<>();
                int count = 0;

                for(int i = 0; i < s1.length(); i++) 
                        mapS1.put(s1.charAt(i), mapS1.getOrDefault(s2.charAt(i), 0) + 1);

                for(int i = 0; i < s2.length(); i++) 
                        mapS1.put(s2.charAt(i), mapS2.getOrDefault(s2.charAt(i), 0) + 1);

                for(Map.Entry<Character, Integer> entry : mapS1.entrySet()) {
                        int a = mapS1.get(entry.getKey());
                        int b = mapS2.get(entry.getKey());

                        if(a > 0 && b >= 0) count += Math.min(a, b);
                } 

                return count;
}

CORRECTED AND WORKING VERSION: Thank you everyone!

static int res(String s1, String s2) {
                HashMap<Character, Integer> mapS1 = new HashMap<>();
                HashMap<Character, Integer> mapS2 = new HashMap<>();
                int count = 0;

                for(int i = 0; i < s1.length(); i++)
                        mapS1.put(s1.charAt(i), mapS1.getOrDefault(s1.charAt(i), 0) + 1);
                for(int i = 0; i < s2.length(); i++)
                        mapS2.put(s2.charAt(i), mapS2.getOrDefault(s2.charAt(i), 0) + 1);

                for(Map.Entry<Character, Integer> entry : mapS1.entrySet()) {
                        int a = mapS1.getOrDefault(entry.getKey(), 0);
                        int b = mapS2.getOrDefault(entry.getKey(), 0);
                        System.out.println(a + " " + b);
                        count += Math.min(a, b);
                }

                return count;
        }
```


Amongalen :

There are two issues in your code. First, "less important" is the copy-paste error. In the second loop you're adding elements to the mapS1 again, I believe it was meant to be mapS2.

As you've mentioned in the comment, the main problem is the NullPointerException you're getting in this line:

int b = mapS2.get(entry.getKey());

This is caused by the fact that if there isn't any value for a given key it returns null. Then that null is supposed to be assigned to b which is an int. int variables doesn't accept null values and thus throws NPE. The easiest solution is to return a default value if there isn't any mapping present. You can do it like this:

int b = mapS2.getOrDefault(entry.getKey(), 0);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=299730&siteId=1