|
| 1 | +# Given two strings s and t, return true if t is an anagram of s, and false otherwise. |
| 2 | + |
| 3 | +# Example 1: |
| 4 | +# Input: s = "anagram", t = "nagaram" |
| 5 | +# Output: true |
| 6 | + |
| 7 | +# Example 2: |
| 8 | +# Input: s = "rat", t = "car" |
| 9 | +# Output: false |
| 10 | + |
| 11 | +# Constraints: |
| 12 | +# 1 <= s.length, t.length <= 5 * 104 |
| 13 | +# s and t consist of lowercase English letters. |
| 14 | + |
| 15 | +# ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 16 | + |
| 17 | +# SORTING |
| 18 | +# compare the sorted strings |
| 19 | +# if the sorted strings are equal, they are anagrams, so return True, else return False |
| 20 | +# it is optional to check for lengths |
| 21 | + |
| 22 | +class Solution: |
| 23 | + def isAnagram(self, s: str, t: str) -> bool: |
| 24 | +# if len(s) != len(t): |
| 25 | +# return False |
| 26 | + |
| 27 | + return sorted(s) == sorted(t) |
| 28 | + |
| 29 | +Time Complexity = O(nlogn + mlogm) where n is length of s and m is length of t |
| 30 | +Space Complexity = O(n + m) or O(1) depending on the sorting algorithm- (ChatGPT says O(n + m)) |
| 31 | + |
| 32 | +# ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 33 | + |
| 34 | +# HASH TABLES: Maintain a frequency table of each letter in the strings |
| 35 | +# first compare length of strings; if they are equal then go ahead |
| 36 | +# create 2 empty hash tables; 1 for each string |
| 37 | +# iterate through the length of the string(any string, because both lengths are same) |
| 38 | +# when the loop is on a character, add 1 to the count. But what if that character is met for the first time? |
| 39 | +# use hashtable.get() method, where 1st parameter is the character(s[i]), 2nd is the default value(0). |
| 40 | +# 0 because we are adding 1 to it when it's encountered. |
| 41 | +# compare both the hashtables. If they are not equal, they are not anagrams, so return False |
| 42 | + |
| 43 | +class Solution: |
| 44 | + def isAnagram(self, s: str, t: str) -> bool: |
| 45 | + if len(s) != len(t): |
| 46 | + return False |
| 47 | + |
| 48 | + countS, countT = {}, {} |
| 49 | + |
| 50 | + for i in range(0, len(s)): |
| 51 | + countS[s[i]] = 1 + countS.get(s[i], 0) |
| 52 | + countT[t[i]] = 1 + countT.get(t[i], 0) |
| 53 | + |
| 54 | +# To compare both the hash tables, start a loop. |
| 55 | +# If the value(freq) of a key(character) in hash table S is not equal to that in T, return False. |
| 56 | +# But what if that character is not in T at all? The comparison will throw error. So set a default value of that character to 0. |
| 57 | +# If the loop ends without returning false, that means they are anagrams, so return True |
| 58 | + |
| 59 | + # for c in countS: |
| 60 | + # if countS[c] != countT.get(c, 0): |
| 61 | + # return False |
| 62 | + |
| 63 | + # return True |
| 64 | + |
| 65 | + return countS == countT |
| 66 | + |
| 67 | +Time Complexity = O(n + m) |
| 68 | +Space Complexity = O(1) since we have at most 26 different characters |
| 69 | + |
| 70 | +# Companies: |
| 71 | +# Bloomberg- 15 |
| 72 | +# Google- 11 |
| 73 | +# Amazon- 8 |
| 74 | +# Microsoft- 5 |
| 75 | +# Meta- 3 |
| 76 | +# Apple- 3 |
| 77 | +# Affirm- 3 |
| 78 | +# Uber- 3 |
| 79 | +# EPAM Systems- 2 |
| 80 | +# Yelp- 2 |
| 81 | +# Adobe- 12 |
| 82 | +# Yahoo- 9 |
| 83 | +# TCS- 5 |
| 84 | +# Accenture- 5 |
| 85 | +# Oracle- 3 |
| 86 | +# Goldman Sachs- 3 |
| 87 | +# JP Morgan- 2 |
| 88 | +# Walmart Labs- 2 |
| 89 | +# Tiktok- 2 |
0 commit comments