1/8
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
Mastercard Values
Create Value, Grow Together, Move Fast
Given an integer array nums, return true if any value appears more than once in the array, otherwise return false.
Example 1:
Input: nums = [1, 2, 3, 3]
Output: TrueExample 2:
Input: nums = [1, 2, 3, 4]
Output: Falseclass Solution:
def hasDuplicate(self, nums: List[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False#What Is The Time Complexity Of This?
class Solution:
def hasDuplicate(self, nums: List[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return FalseO(n)
#What Is The Space Complexity Of This?
class Solution:
def hasDuplicate(self, nums: List[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return FalseO(n)
# Thought Process Behind This Code
class Solution:
def hasDuplicate(self, nums: List[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return FalseTo check for duplicates, I use a set called seen, which starts empty. I iterate through the list and, for each value, I first check whether it is already in the set. If so, a duplicate exists, so I return true immediately. If it isn’t, I add the value to the set and continue. If the loop finishes without finding any duplicates, I return false
Given two strings s and t, return true if the two strings are anagrams of each other, otherwise return false.
An anagram is a string that contains the exact same characters as another string, but the order of the characters can be different.
Example 1:
Input: s = "racecar", t = "carrace"
Output: trueExample 2:
Input: s = "jar", t = "jam"
Output: falseConstraints:
s and t consist of lowercase English letters.
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
countS, countT = {}, {}
for i in range(len(s)):
countS[s[i]] = 1 + countS.get(s[i],0)
countT[t[i]] = 1 + countT.get(t[i],0)
for c in countS:
if countS[c] != countT.get(c,0):
return False
return True#Time Complexity Of This Code?
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
countS, countT = {}, {}
for i in range(len(s)):
countS[s[i]] = 1 + countS.get(s[i],0)
countT[t[i]] = 1 + countT.get(t[i],0)
for c in countS:
if countS[c] != countT.get(c,0):
return False
return TrueO(n)
#Space Complexity Of This Code?
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
countS, countT = {}, {}
for i in range(len(s)):
countS[s[i]] = 1 + countS.get(s[i],0)
countT[t[i]] = 1 + countT.get(t[i],0)
for c in countS:
if countS[c] != countT.get(c,0):
return False
return TrueO(n)
#Thought Process Of This Code?
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
countS, countT = {}, {}
for i in range(len(s)):
countS[s[i]] = 1 + countS.get(s[i],0)
countT[t[i]] = 1 + countT.get(t[i],0)
for c in countS:
if countS[c] != countT.get(c,0):
return False
return TrueFirst, I check if the strings have the same length — if not, they can’t be anagrams. Then I count the frequency of each character in both strings using dictionaries. Finally, I compare the counts: if every character in the first string has the same frequency in the second, I return True; otherwise, I return False.