如何O(1)复杂度比较两个Hashmap相等# JobHunting - 待字闺中
M*r
1 楼
有一道题,要maintain一个sliding window,这个window用Hashmap来实现,
但是java里面hashmap.equals复杂度是O(n)
原题
Given a list of keywords and a list of search words, return a list of
indices that indicate the beginning of sequences of adjacent keywords.
Examples:
Search list: ['hello', 'hi', 'welcome', 'greetings', 'hi', 'greetings', 'hey
', 'hello']
Keywords: ['hi', 'hey', 'greetings']. from: 1point3acres.com/bbs
Output: [4]
Search list: ['i', 'saw', 'susie', 'sitting', 'in', 'a', 'shoe', 'shine', '
shop', 'where', 'she', 'sits', 'she', 'shines', 'and', 'where', 'she', '
shines', 'she', 'sits']
Keywords: ['she', 'sits', 'shines']
Output: [11, 17]
Search list: ['peter', 'piper', 'picked', 'a', 'peck', 'of', 'pickled', '
peppers', 'a', 'peck', 'of', 'pickled', 'peppers', 'peter', 'piper',’ '
picked', 'if', 'peter', 'piper', 'picked', 'a', 'peck', 'of', 'pickled', '
peppers', 'wheres', 'the', 'peck', 'of', 'pickled', 'peppers', 'peter', '
piper', 'picked']
Keywords: ['peter', 'picked', 'piper']
Output: [0, 13, 17, 31]
但是java里面hashmap.equals复杂度是O(n)
原题
Given a list of keywords and a list of search words, return a list of
indices that indicate the beginning of sequences of adjacent keywords.
Examples:
Search list: ['hello', 'hi', 'welcome', 'greetings', 'hi', 'greetings', 'hey
', 'hello']
Keywords: ['hi', 'hey', 'greetings']. from: 1point3acres.com/bbs
Output: [4]
Search list: ['i', 'saw', 'susie', 'sitting', 'in', 'a', 'shoe', 'shine', '
shop', 'where', 'she', 'sits', 'she', 'shines', 'and', 'where', 'she', '
shines', 'she', 'sits']
Keywords: ['she', 'sits', 'shines']
Output: [11, 17]
Search list: ['peter', 'piper', 'picked', 'a', 'peck', 'of', 'pickled', '
peppers', 'a', 'peck', 'of', 'pickled', 'peppers', 'peter', 'piper',’ '
picked', 'if', 'peter', 'piper', 'picked', 'a', 'peck', 'of', 'pickled', '
peppers', 'wheres', 'the', 'peck', 'of', 'pickled', 'peppers', 'peter', '
piper', 'picked']
Keywords: ['peter', 'picked', 'piper']
Output: [0, 13, 17, 31]