-
Leetcode Weekly Contest 150
Find Words That Can Be Formed by Characters For this problem, we can simply use a hashmap to count the number of each character appears in chars. For each word in words, we check if the number of each character appears in word in less than or equal to the number in original chars. Let n be the number of word in words and m be the length of each word. The time complexity is O(mn) and space complexity is O(26) = O(1). def countCharacters(self, words, chars): """ :type words: List[str] :type chars: str :rtype: int """ # Use chars_map to count the number of each char appears in chars chars_map = {} for c in chars: if c not in chars_map: chars_map[c] = 0 chars_map[c] += 1 ans = 0 # For each word in words, we compare the number of each character for word in words: word_map = {} for c in word: if c not in word_map: word_map[c] = 0 word_map[c] += 1 flag = 0 # early stop if c not in chars_map or word_map[c] > chars_map[c]: flag = 1 break if not flag: ans += len(word) return ans …
-
Sample Markdown Post
You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different ways, but the most common way is to run jekyll serve --watch, which launches a web server and auto-regenerates your site when a file is updated. …