-
LeetCode 1207: Unique Number of Occurrences
link We can create a map: {value -> count}. Each value having unique number of occurrences means: in the map, each value has a distinct count. In other words, the number of different counts in the map is same as the map size. Time: , space: .
-
LeetCode 409: Longest Palindrome
link We can have at most one character with odd number of occurrences in a palindrome. So, in one pass we find the count of characters that appear odd number of times. Time: , space: .
-
LeetCode 1915: Number of Wonderful Substrings
link For each substring, we can check if it is wonderful. Time: , space: . We can optimize is_wonderful by keeping track of the count of characters that appear odd number of times while building the char_count map. Using the same idea for each begin, we can get rid of is_wonderful. Time: , space: .
-
LeetCode 694: Number of Distinct Islands
link The challenge is to find a translation-invariant hash function for islands. We can consider each island having a bounding box around it and then we can translate that bounding box to the top-left of the grid. In this translated bounding box, if for two islands, the sequence of 1-cells are identical, we consider them…
-
LeetCode 791: Custom Sort String
link We can use counting sort. While processing order left-to-right, if order[i] = and appears times in s, we emit [c] * m. Time: , extra space: . Since the alphabet only consists of lowercase English letters, we can keep a list of length as the s_map.
-
LeetCode 299: Bulls and Cows
link If secret[i] = guess[i] that is a bull. So, we can process and exclude bull positions. In the remaining positions if guess[i] could be matched with a secret[j], that is a cow. So, we use two passes: (1) Bull pass (2) Cow pass. Time , space: . Note, len(secret) = len(guess). Say for a…
-
LeetCode 1086: High Five
link We create a map: {id -> minq(score)} and we cap the length of the minq at 5. In that way, in the end, we have the top five scores per id. Say len(items) = . There are two extreme cases: Time: , space: . Note, the heap plays a role only if there are…
-
LeetCode 1570: Dot Product of Two Sparse Vectors
link For two vectors and , the dot product . So, we represent a sparse vector as a map of nonzero values: {index -> value}. While computing dot product, we iterate over the vector that has a shorter sparse representation. Say len(nums) = and only values are nonzero. Operation Time Space __init__ dotProduct Follow up: What…
-
LeetCode 609: Find Duplicate File in System
link We build a map: content -> [file_path]. If there are more than one files against a content, they are duplicates. Say there are files and the longest file path has length , then time: , space is proportional to the total number of characters including contents. Follow-up Imagine you are given a real file…
-
LeetCode 205: Isomorphic Strings
link Two strings are isomorphic if we can find a two-way mapping between their characters that makes the two strings identical. We keep two maps to validate the two-way mapping. Two maps we maintain are: s -> t and t -> s. For s[i] and t[i], either they have not been mapped yet or s[i]…