LeetCode - Blind 75 - Group Anagrams

The problem Given an array of strings strs, group the anagrams together. You can return the answer in any order. An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once. Examples 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. Input: strs = [""] Output: [[""]] Input: strs = ["a"] Output: [["a"]] Constraints 1 <= strs.length <= 1000 0 <= strs[i].length <= 100 strs[i] is made up of lowercase English letters. Brute force solution func groupAnagrams(_ strs: [String]) -> [[String]] { var res: [String: [String]] = [:] for s in strs { let sortedS = String(s.sorted()) res[sortedS, default: []].append(s) } return Array(res.values) } Explanation The brute force solution: ...

November 1, 2024 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Valid Anagram

The Problem Given two strings s and t, return true if t is an anagram of s, and false otherwise. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, using all the original letters exactly once. Examples Input: s = "anagram", t = "nagaram" Output: true Input: s = "rat", t = "car" Output: false Follow-up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case? ...

October 30, 2024 · 3 min · Dmytro Chumakov

LeetCode - Blind 75 - Contains Duplicate

The Problem Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. Example Input: nums = [1,2,3,1] Output: true Explanation: The element 1 occurs at indices 0 and 3. Follow-up: Can you come up with an algorithm that has less than O(NlogN) time complexity? Brute Force Solution func containsDuplicate(_ nums: [Int]) -> Bool { var nums = nums let N = nums.count if N == 0 { return false } nums.sort() var prev = nums[0] for i in 1 ..< N { let num = nums[i] if num == prev { return true } else { prev = num } } return false } Explanation The brute force solution for this problem uses sorting to allow us to quickly detect duplicates since identical elements will be adjacent. After sorting, it iterates through all elements, checking if the current element is equal to the previous one; if it is, it returns true immediately. If not, it updates prev. If no duplicates are detected, it returns false. ...

October 26, 2024 · 2 min · Dmytro Chumakov

LeetCode - Blind 75 - Two Sum

The Problem Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to the 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 Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Follow-up: Can you come up with an algorithm that has less than O(N^2) time complexity? ...

October 25, 2024 · 2 min · Dmytro Chumakov

DSA - Sorting - Selection Sort

Introduction The selection sort is an in-place comparison sorting algorithm. It’s similar to bubble sort in that it works by repeatedly swapping items in a list and not very efficient on larger lists. source Code Example func selectionSort(_ array: [Int]) -> [Int] { var A = array let N = array.count for i in 0 ..< N { var jMin = i for j in (i + 1) ..< N { if A[j] < A[jMin] { jMin = j } } if jMin != i { let tmp = A[i] A[i] = A[jMin] A[jMin] = tmp } } return A } Implementation For each index: Set jMin index to the current index For each index from i + 1 to the end of the list: If the element at inner index j is less than the element at index jMin, set jMin to j If jMin does not equal i, swap the element at the current index i with the element at index jMin Time/Space Complexity Time complexity: O(N^2) Space complexity: O(1) auxiliary ...

October 23, 2024 · 1 min · Dmytro Chumakov