Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote.
# Time complexity: O(n)# Space complexity: O(n)defcanConstruct(ransomNote:str, magazine:str)->bool: m ={}for c in magazine: m[c]= m.get(c,0)+1for c in ransomNote:if c notin m or m[c]==0:returnFalse m[c]-=1returnTrue
Given two strings s and t, determine if they are isomorphic.
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself. Assume s.length == t.length.
# Time complexity: O(n)# Space complexity: O(n)defisIsomorphic(self, s:str, t:str)->bool: s2t, t2s ={},{}for i inrange(len(s)):# Characters must have the same mapping in both directionsif s[i]in s2t and s2t[s[i]]!= t[i]:returnFalseif t[i]in t2s and t2s[t[i]]!= s[i]:returnFalse s2t[s[i]]= t[i] t2s[t[i]]= s[i]returnTrue
NOTE: The solution for "Word Pattern" can also be used to solve this problem.
# Time complexity: O(n)# Space complexity: O(n)# Example: "abba" -> (0, 1, 1, 0)defconvertPatternToTuple(string:str)->tuple: m ={} count =0 result =[]for c in string:if c in m: result.append(m[c])else: m[c]= count
result.append(count) count +=1returntuple(result)# Example: "dog cat cat dog" -> (0, 1, 1, 0)defconvertWordsToTuple(words:str)->tuple: m ={} count =0 result =[] words = words.split()for word in words:if word in m: result.append(m[word])else: m[word]= count
result.append(count) count +=1returntuple(result)defwordPattern(pattern:str, s:str)->bool:return convertPatternToTuple(pattern)== convertWordsToTuple(s)
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, typically using all the original letters exactly once.
# Time complexity: O(n)# Space complexity: O(n)defisAnagram(s:str, t:str)->bool:iflen(s)!=len(t):returnFalse m ={}for c in s: m[c]= m.get(c,0)+1for c in t:if c notin m or m[c]==0:returnFalse m[c]-=1returnTrue
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.
# Time complexity: O(n)# Space complexity: O(n)deftwoSum(nums: List[int], target:int)-> List[int]: m ={}for i inrange(len(nums)): comp = target - nums[i]if comp in m:return[i, m[comp]] m[nums[i]]= i
Write an algorithm to determine if a number n is happy.
A happy number is a number defined by the following process:
Starting with any positive integer, replace the number by the sum of the squares of its digits.
Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
Those numbers for which this process ends in 1 are happy.
Return true if n is a happy number, and false if not.
# Time complexity: O(n)# Space complexity: O(n)defisHappy(n:int)->bool: m =set()whileTrue:if n in m:returnFalseelse: m.add(n) li =list(str(n))sum=0for c in li:sum+=int(c)**2ifsum==1:returnTrue n =sum
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.
# Time complexity: O(n)# Space complexity: O(n)defcontainsNearbyDuplicate(nums: List[int], k:int)->bool: m ={}for i inrange(len(nums)):if nums[i]notin m: m[nums[i]]= i
else:ifabs(i - m[nums[i]])<= k:returnTrue m[nums[i]]= i
returnFalse
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, typically using all the original letters exactly once.
# Time complexity: O(n * m * log(m))# Space complexity: O(n * m)defgroupAnagrams(strs: List[str])-> List[List[str]]: answer ={}for word in strs: w ="".join(sorted(word))if w in answer: answer[w].append(word)else: answer[w]=[word]returnlist(answer.values())
Alternative solution:
# Time complexity: O(n * m)# Space complexity: O(n * m)defgroupAnagrams(strs: List[str])-> List[List[str]]: m ={}for word in strs:# Use list to count char frequency chars =[0]*26for char in word: chars[ord(char)-97]+=1# Use frequency as map key tup =tuple(chars)if tup in m: m[tup].append(word)else: m[tup]=[word]return[m[tup]for tup in m]
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
Write an algorithm that runs in O(n) time.
# Time complexity: O(n)# Space complexity: O(n)deflongestConsecutive(nums: List[int])->int: s =set(nums) answer =0for n in nums:# n is lowest value aka start of own sequenceif n -1notin s: end = n +1while end in s: end +=1 answer =max(answer, end - n)return answer