242. Valid Anagram
LeetCode's Valid Anagram problem checks if two strings, `s` and `t`, are anagrams by comparing character frequencies.

Search for a command to run...
LeetCode's Valid Anagram problem checks if two strings, `s` and `t`, are anagrams by comparing character frequencies.

No comments yet. Be the first to comment.
Step 1: Accessing the AWS Amplify Console Our journey begins in the AWS Management Console. To start, first choose a Region. This is an important decision as it determines the location of your application's infrastructure. Consider factors like your ...

[A1] - Brute Force Time: O(n^2) Space: O(n^2) class Solution: def generate(self, numRows: int) -> List[List[int]]: # Initialize the triangle with the first row pascal_triangle = [] for i in range(numRows): # S...

YouTube Video https://youtu.be/hfN4VONP4HQ [A1] - Brute Force Time: O(n^2) Space: O(1) class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: for i in range(len(nums)): for j in range(i+1, len(nums)): ...

LeetCode's Contains Duplicate problem checks if there are any duplicate elements in a given array, often solved using hash sets or sorting.

Time: O(nlog(n))
Space: O(1)
class Solution(object):
def isAnagram(self, s, t):
s_sorted = sorted(s)
t_sorted = sorted(t)
if s_sorted == t_sorted:
return True
else:
return False
class Solution(object):
def isAnagram(self, s, t):
return sorted(s)==sorted(t) # comparing two lists
Comment: In python, string is immutable. That is why here I am using sorted() function instead of sort() as sorted() returns a completely new string and don’t modify the original string.
Time: O(n)
Space: O(n)
class Solution(object):
def isAnagram(self, s, t):
if len(s)!=len(t):
return False
ds, dt= {}, {}
for i in range(len(s)):
ds[s[i]] = 1 + ds.get(s[i],0)
dt[t[i]] = 1 + dt.get(t[i],0)
for c in ds:
if ds[c] != dt.get(c,0):
return False
return True
Comment: Here we are building our own hash table and checking if the count of all the elements is same.
Time: O(n)
Space: O(n)
class Solution(object):
def isAnagram(self, s, t):
return Counter(s)==Counter(t)
Comment: This approach works exactly same as A2. We are just using the Counter() function which is handling everything by itself. This approach is probably not suitable for interview.