LeetCode - Blind 75 - Product of Array Except Self
The problem Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]. The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer. You must write an algorithm that runs in O(n) time and without using the division operation. Examples Input: nums = [1,2,3,4] Output: [24,12,8,6] Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0] Constraints 2 <= nums....
LeetCode - Blind 75 - Top K Frequent Elements
The problem Given an integer array聽nums聽and an integer聽k, return the聽k聽most frequent elements. You may return the answer in any order. Examples Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Input: nums = [1], k = 1 Output: [1] Constraints 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 k聽is in the range聽[1, the number of unique elements in the array]. It is聽guaranteed聽that the answer is unique....
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....
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?...
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....