-
LeetCode 2131: Longest Palindrome by Concatenating Two Letter Words
link From palindrome’s mirror symmetry point of view, there are two types of elements: We find the longest length in two passes: (1) Build {element -> count} map (2) Depending on if it is a “repeated” element or not, we include them appropriately. If there are elements, time: , space: .
-
LeetCode 438: Find All Anagrams in a String
link An anagram of p is a window in s, so we can use sliding window. Variable-length window We expand the window until we get at least the required count for each character in p. Then we shrink to get the window size right. If the shrunk window still has all characters by the required…
-
LeetCode 387: First Unique Character in a String
link The first character in the string that appears once is our answer. So, we use two passes: (1) Create the map {char -> count} (2) Find first character having count = 1. Time: , space: .
-
LeetCode 895: Maximum Frequency Stack
link Since a tie for the maximum frequency must be broken in favor of recent-most value, we should maintain LIFO order per frequency. So, we need to maintain a stack per color. We shall use the below three pieces of information: Say there are numbers in the FreqStack having distinct values. So, . Operation Time…
-
LeetCode 49: Group Anagrams
link To group anagrams, we need a group id against which we can collect the anagrams. Sort Since all anagrams are identical after sorting, we can use sorted anagram as the group id. If there are strings and the longest string has length , time: , space: . Character frequency Sequence of character counts, in…
-
LeetCode 348: Design Tic-Tac-Toe
link For each player we can keep four counts: (1) per row (2) per col (3) the major diagonal (4) the minor diagonal. After the move, if any of these four counts is , the player won. Operation Time Space __init__ move For better readability we could encapsulate counts in a Player class.
-
LeetCode 242: Valid Anagram
link Sort Sorted, anagrams must be identical. Time: , space: . Character map Every character of t must appear in s possibly in different order. Time: , space: .
-
LeetCode 266: Palindrome Permutation
link As we build {char -> count} map, we can find out the count of characters that appear odd number of times. For a palindrome there can be at most 1 such character. Time: , space: . We can also maintain a set of characters that appear odd number of times.
-
LeetCode 2262: Total Appeal of A String
link Try all substrings For a substring with end-points: (begin, end), length of the set() of characters is the appeal. Time: , space: . Prefix appeals Above, we are already quite efficient, taking only time per substring. So, if we want to be more efficient, say take total time, we cannot iterate over all substrings.…
-
LeetCode 523: Continuous Subarray Sum
link Try all subarrays A subarray has a start index and an end index. With all possible starts and ends we check if the sum is a multiple of . Time: , space: . Prefix sums Say is the sum of the subarray nums[j : k+1]. Then, it would be a good subarray if .…