diff --git a/content/SoftwareEngineering/Leetcode/0003-Two Sum.md b/content/SoftwareEngineering/Leetcode/0003-Two Sum.md new file mode 100644 index 000000000..f0f2a1a3e --- /dev/null +++ b/content/SoftwareEngineering/Leetcode/0003-Two Sum.md @@ -0,0 +1,122 @@ + +[1. Two Sum](https://leetcode.com/problems/two-sum/) + +Easy + +Topics + +Companies + +Hint + +Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_. + +You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice. + +You can return the answer in any order. + +**Example 1:** + +**Input:** nums = [2,7,11,15], target = 9 +**Output:** [0,1] +**Explanation:** Because nums[0] + nums[1] == 9, we return [0, 1]. + +**Example 2:** + +**Input:** nums = [3,2,4], target = 6 +**Output:** [1,2] + +**Example 3:** + +**Input:** nums = [3,3], target = 6 +**Output:** [0,1] + +**Constraints:** + +- `2 <= nums.length <= 104` +- `-109 <= nums[i] <= 109` +- `-109 <= target <= 109` +- **Only one valid answer exists.** + +**Follow-up:** Can you come up with an algorithm that is less than `O(n2)` time complexity? + + +To solve the "Two Sum" problem, we need to find two distinct indices in the array where the sum of the numbers at those indices equals the target. + +### Approach: + +1. **Brute Force (O(n²)):** + - Check every possible pair of numbers and see if their sum equals the target. + - However, the brute force approach isn't efficient for large inputs, as it has a time complexity of \(O(n^2)\). + +2. **Optimal Approach using HashMap (O(n)):** + - We can solve this in linear time using a hash map (or dictionary in Python). + - As we iterate through the array, we check if the complement (i.e., `target - current number`) exists in the hash map. + - If it exists, we found our pair; otherwise, we store the current number along with its index in the hash map. + - This approach works in \(O(n)\) time since we only pass through the array once. + +### Algorithm: + +1. Create an empty hash map (dictionary) to store numbers and their indices. +2. Iterate through the `nums` array. +3. For each element, compute its complement by subtracting the current element from the target (`complement = target - nums[i]`). +4. Check if the complement exists in the hash map. + - If it does, return the indices of the current element and the complement. + - If not, store the current element and its index in the hash map. +5. Continue this process until the solution is found. + +### Code Implementation: + +```python +def twoSum(nums, target): + # Dictionary to store the value and its index + hashmap = {} + + # Iterate through the array + for i, num in enumerate(nums): + # Calculate the complement + complement = target - num + + # Check if complement is in the hashmap + if complement in hashmap: + # Return the indices if complement is found + return [hashmap[complement], i] + + # Store the current number and its index in the hashmap + hashmap[num] = i + +# Test cases +print(twoSum([2, 7, 11, 15], 9)) # Output: [0, 1] +print(twoSum([3, 2, 4], 6)) # Output: [1, 2] +print(twoSum([3, 3], 6)) # Output: [0, 1] +``` + +### Explanation: + +- **nums = [2, 7, 11, 15], target = 9**: + - Iteration 1: `num = 2`, `complement = 7`. Hash map is `{}`. Store `2` with index `0` → `{2: 0}`. + - Iteration 2: `num = 7`, `complement = 2`. Found `2` in the hash map! Return `[0, 1]`. + +- **nums = [3, 2, 4], target = 6**: + - Iteration 1: `num = 3`, `complement = 3`. Hash map is `{}`. Store `3` with index `0` → `{3: 0}`. + - Iteration 2: `num = 2`, `complement = 4`. Store `2` with index `1` → `{3: 0, 2: 1}`. + - Iteration 3: `num = 4`, `complement = 2`. Found `2` in the hash map! Return `[1, 2]`. + +### Time Complexity: + +- **Time Complexity**: \(O(n)\), where \(n\) is the length of the input array `nums`. We only iterate through the array once. +- **Space Complexity**: \(O(n)\), because in the worst case, we need to store all the elements in the hash map. + +This solution is efficient and solves the problem in linear time. +```python +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + prevMap = {} # val -> index + + for i, n in enumerate(nums): + diff = target - n + if diff in prevMap: + return [prevMap[diff], i] + prevMap[n] = i + +``` diff --git a/content/SoftwareEngineering/Leetcode/0004- Group Anagram.md b/content/SoftwareEngineering/Leetcode/0004- Group Anagram.md new file mode 100644 index 000000000..4c286d856 --- /dev/null +++ b/content/SoftwareEngineering/Leetcode/0004- Group Anagram.md @@ -0,0 +1,131 @@ +[49. Group Anagrams](https://leetcode.com/problems/group-anagrams/) + +Medium + +Topics + +Companies + +Given an array of strings `strs`, group the  + +anagrams + + together. You can return the answer in **any order**. + +**Example 1:** + +**Input:** strs = ["eat","tea","tan","ate","nat","bat"] + +**Output:**  [["bat"],["nat","tan"],["ate","eat","tea"]] + +**Explanation:** + +- There is no string in strs that can be rearranged to form `"bat"`. +- The strings `"nat"` and `"tan"` are anagrams as they can be rearranged to form each other. +- The strings `"ate"`, `"eat"`, and `"tea"` are anagrams as they can be rearranged to form each other. + +**Example 2:** + +**Input:** strs = [""] + +**Output:** [[""]] + +**Example 3:** + +**Input:** strs = ["a"] + +**Output:** [["a"]] + +**Constraints:** + +- `1 <= strs.length <= 104` +- `0 <= strs[i].length <= 100` +- `strs[i]` consists of lowercase English letters. + +To solve the "Group Anagrams" problem, we need to group strings that are anagrams of each other. Two strings are anagrams if they contain the same characters in the same frequencies, but possibly in a different order. + +### Approach: + +1. **Key Idea:** + - We can use sorting to identify anagrams. If two strings are anagrams, their sorted versions will be identical. + - Alternatively, we can use a frequency count of characters as a signature for each string. + +2. **Optimal Approach:** + - Create a dictionary where the key is a signature of the string (sorted characters or character counts), and the value is a list of strings that share that signature. + - Iterate through the list of strings, compute the signature for each, and group them into the dictionary. + - Finally, return the values of the dictionary as the grouped anagrams. + +### Solution using Sorted Strings: + +We can use sorted strings as the key for each group of anagrams. By sorting the characters in each string, we can group anagrams together. + +### Algorithm: + +1. Create an empty dictionary to store the groups of anagrams. +2. For each string in `strs`, sort its characters and use the sorted string as a key in the dictionary. +3. Append the original string to the list corresponding to that key. +4. After processing all strings, return the values of the dictionary, which are the grouped anagrams. + +### Code Implementation: + +```python +def groupAnagrams(strs): + # Dictionary to store groups of anagrams + anagram_map = {} + + # Iterate through each string in the input list + for s in strs: + # Sort the string to use as the key + sorted_str = ''.join(sorted(s)) + + # Add the string to the corresponding anagram group + if sorted_str not in anagram_map: + anagram_map[sorted_str] = [] + anagram_map[sorted_str].append(s) + + # Return all grouped anagrams + return list(anagram_map.values()) + +# Test cases +print(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])) +# Output: [["bat"], ["nat","tan"], ["ate","eat","tea"]] + +print(groupAnagrams([""])) +# Output: [[""]] + +print(groupAnagrams(["a"])) +# Output: [["a"]] +``` + +### Explanation: + +- **strs = ["eat", "tea", "tan", "ate", "nat", "bat"]**: + - For each string, we sort its characters: + - "eat" → "aet" + - "tea" → "aet" + - "tan" → "ant" + - "ate" → "aet" + - "nat" → "ant" + - "bat" → "abt" + - The dictionary after processing all strings: + ```python + { + "aet": ["eat", "tea", "ate"], + "ant": ["tan", "nat"], + "abt": ["bat"] + } + ``` + - The final result is `[['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]`. + +- **strs = [""]**: + - The sorted version of the empty string is also an empty string, so the output is `[['']]`. + +- **strs = ["a"]**: + - The sorted version of "a" is "a", so the output is `[['a']]`. + +### Time Complexity: + +- **Time Complexity**: \(O(n \cdot k \log k)\), where \(n\) is the number of strings, and \(k\) is the maximum length of a string. Sorting each string takes \(O(k \log k)\), and we do this for all \(n\) strings. +- **Space Complexity**: \(O(n \cdot k)\), where \(n\) is the number of strings and \(k\) is the maximum length of a string, as we store the strings in a dictionary. + +This solution is efficient and works well within the problem constraints. \ No newline at end of file