Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

Valid Anagram (easy)

Problem Statement

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.

Example 1:

Input: s = "listen", t = "silent"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: false

Example 3:

Input: s = "hello", t = "world"
Output: false

Constraints:

  • 1 <= s.length, t.length <= 5 * 10<sup>4</sup>
  • s and t consist of lowercase English letters.

.....

.....

.....

Like the course? Get enrolled and start learning!
E

Ejike Nwude

· 3 years ago

Your testcases allow the following solution to pass when it should fail. For instance the following are not anagrams {add, ada} but since a Set does not account for the frequency, it passes them as anagrams:

import java.util.Arrays; import java.util.HashSet; import java.util.Set; class Solution {     public boolean isAnagram(String s, String t) {         if (s.length() != t.length()) {             return false;         }         Set<Character> characters = new HashSet<>();         for (char c : s.toCharArray()) {             characters.add(c);         }         for (char character : t.toCharArray()) {             if (!characters.contains(character)) {                 return false;             }         }         return true;     } }
Show 1 reply
Made Doddy Adi Pranatha

Made Doddy Adi Pranatha

· 3 years ago

function isAnagram(s, t) {     // TODO: Write your code here     return s.split("").sort().join("") === t.split("").sort().join(""); }
T

Thai Minh

· 3 years ago

class Solution: def isAnagram(self, s, t): # TODO: Write your code here # create dict for string s # check char of t for s dict if it is not in s then false s_dict = {} for ch in s: s_dict[ch] = s_dict.get(ch, 0) + 1 for ch in t: if ch not in s: return False elif ch in s and s_dict[ch] - 1 < 0: return False else: s_dict[ch] -= 1 return True
A

amazonintern101

· 3 years ago

class Solution: def isAnagram(self, s, t): # TODO: Write your code here charSet = {} if len(s) != len(t): return False for char in s: if char not in charSet: charSet[char] = 0 else: charSet[char] += 1 for char in t: if char not in charSet: return False else: if charSet[char] >= 0: charSet[char] -= 1 else: return False return True
Alex

Alex

· 3 years ago

By definition an anagram has to be a word or set of words that can be re-arranged into another word or set of words. An empty string isn't a word and can't be rearranged

Show 1 reply
H

hassan.javeed84

· 2 years ago

This code passes all the tests however it's wrong as in case of s = "hel" and t = "hll" it returns True

class Solution: def isAnagram(self, s, t): # TODO: Write your code here if len(s) != len(t): return False l = list(s) for c in t: if c not in l: return False return True
Show 1 reply
Amber Wolf

Amber Wolf

· a year ago

This was my solution to the question. There is however one test case in the web browser IDE that is incorrect. In my solution I accounted for a string that can be given that is empty, which by definition is not an anagram. So a string "" being compared to any other string, including "" should be false as that is not an anagram.

public bool isAnagram(string s, string t) { // using Regex to massage the strings and remove any special characters/white spaces var trimmedS = Regex.Replace(s, "[^a-zA-Z0-9]", "").ToLower(); var trimmedT = Regex.Replace(t, "[^a-zA-Z0-9]", "").ToLower(); // first checking the length of the words, if they are a different amount of charcters or they contain no characters they cannot be an anagram. if (trimmedS.Length != trimmedT.Length
Bruno Raiado

Bruno Raiado

· 4 months ago

using System; using System.Collections.Generic; public class Solution {     public bool isAnagram(string s, string t) {       if(s.Length != t.Length) return false;       int asciiS = 0;       int asciiT = 0;       for(int i =0;i<s.Length;i++) {         asciiS += (int)s[i];         asciiT += (int)t[i];       }       return asciiT == asciiS;     } }
Carol Lisbon

Carol Lisbon

· 4 months ago

class Solution: def isAnagram(self, s, t): if len(s) != len(t): return False s_dict = dict() t_dict = dict() for s1, t1 in zip(s,t): if s1 not in s_dict: s_dict[s1] = 0 if t1 not in t_dict: t_dict[t1] = 0 s_dict[s1] += 1 t_dict[t1] += 1 return s_dict == t_dict
X

xha80n+9p6ne

· 3 months ago

from collections import Counter class Solution: def isAnagram(self, s, t): if len(s) != len(t): return False return Counter(s) == Counter(t)