比较hashMap值

时间:2013-03-22 19:33:36

标签: java hashmap compare

我正在练习一些面试问题,但不知道如何比较hashMap值。前提是你有一本带字符串的杂志。你必须从杂志中删除适当数量的字符以形成赎金票据。我已经设法将字符和字符出现次数添加到hashMap,但是如何比较两个hashMaps以确定我有足够的字母。任何指导都将非常感激。

杂志= {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} 赎金= {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);
    }

3 个答案:

答案 0 :(得分:3)

检查赎金中的每个字母,看看报纸上是否有足够的信件:

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

答案 1 :(得分:1)

安德鲁的回答有效。但我通过使用(测试驱动开发)TDD解决了这个问题。以下是我提出的测试:

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

左边的参数是杂志,右边是赎金。这太大了,不能发表评论,所以我正在使用答案。

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

答案 2 :(得分:0)

如果我正确理解了这个问题。你可以检查所有的字符[a-z A-Z 0-9!@#$%^&amp; )(_ + {:&lt;}“&gt;?]。