Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare hashMap values

Im practicing some interview questions but have no idea how to compare hashMap values. The premise is that you have a magazine with strings. You have to cut out the appropriate number of characters out of the magazine to form a ransom note. I've managed to add both the characters and the number of occurrences of the characters to a hashMap but how do I compare the two hashMaps to determine I have enough letters.Any guidance would be much appreciated.

Magazine = {g=2, =14, d=2, e=2, a=4, n=1, o=5, l=4, m=1, .=1, k=1, I=2, h=2, i=6, w=1, T=1, u=1, t=2, s=3, r=1, y=2} Ransom = {w=1, =3, o=1, l=4, k=1, I=1, y=1, i=2}

String mag = "this is what I said Im going to do. i really like you a lot";
        String ransom = "i will kill you";

        Map<Character,Integer> map = new HashMap<Character,Integer>();
        Map<Character,Integer> ransomMap = new HashMap<Character,Integer>();

        for(int i = 0; i < mag.length() -1; i++)
        {
            char c = mag.charAt(i);
            if(!map.containsKey(c))
            map.put(c, 1);
            else{
                int value = map.get(c);
                map.put(c,++value);
            }
        }

        System.out.println(map);

        for(int i = 0; i < ransom.length()-1; i++ )
        {
        char c = ransom.charAt(i);
        if(!ransomMap.containsKey(c))
            ransomMap.put(c,1);
        else
        {
            int value = (ransomMap.get(c));
            ransomMap.put(c,++value);
        }
        }
        System.out.println(ransomMap);
    }
like image 444
Calgar99 Avatar asked May 09 '26 15:05

Calgar99


2 Answers

Check each letter in your ransom note and see if there are enough in the newspaper:

boolean enoughLetters(Map<Character, Integer> magMap, Map<Character,Integer> ransomMap) {
    for( Entry<Character, Integer> e : ransomMap.entrySet() ) {
        Character letter = e.getKey();
        Integer available = magMap.get(letter);
        if (available == null || e.getValue() > available) return false;
    }
    return true;
}
like image 197
Andrew Mao Avatar answered May 11 '26 05:05

Andrew Mao


Andrew's answer works. But I solved this by using (Test Driven Development) TDD. Here are the tests I came up with:

@Test
public void whenMagazineHasLessCharactersThanRansomThenYouCanCreateRansom() {
    assertFalse(canMakeRansom("abcdef", "abcdefg"));
}

@Test
public void whenMagazineHasSameCharactersOfRansomThenYouCanCreateRansom() {
    assertTrue(canMakeRansom("abcdefg", "abcdefg"));
}

@Test
public void whenMagazineHasSameCharactersOfRansomButInDifferentOrderThenYouCanCreateRansom() {
    assertTrue(canMakeRansom("abcdefg", "gfedcab"));
}

@Test
public void whenMagazineHasSameCharactersOfRansomButHasMoreThenYouCanCreateRansom() {
    assertTrue(canMakeRansom("aabbccdefg", "agfedcab"));
}

@Test
public void whenMagazineHasSameCharactersOfRansomButRansomHasMoreThenYouCantCreateRansom() {
    assertFalse(canMakeRansom("aabbccdefg", "aaaaagfedcab"));
}

The left parameter is the magazine and the right is the ransom. This is too large to post as a comment so I'm using an answer.

private boolean canMakeRansom(String magazine, String ransom) {
    Map<Character, Integer> magList = createCharCountMap(magazine);
    Map<Character, Integer> ransomList = createCharCountMap(ransom);
    return magHasAtLeastTheseCharacters(magList, ransomList);   //Andrew's implementation
}

private Map<Character, Integer> createCharCountMap(String chars) {
    HashMap<Character, Integer> charCountMap = new HashMap<Character, Integer>();
    for (char c : chars.toCharArray()) {
        if (charCountMap.containsKey(c)) {
            charCountMap.put(c, charCountMap.get(c) + 1);
        } else {
            charCountMap.put(c, 1);
        }
    }
    return charCountMap;
}
like image 28
Daniel Kaplan Avatar answered May 11 '26 04:05

Daniel Kaplan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!