简体   繁体   中英

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);
    }

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;
}

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;
}

If i understand the question correctly. you can probabaly check for all the characters [ az AZ 0-9 !@#$%^& ( )(_+{:<}">? ].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM