Algorithm


Problem Name: 242. Valid Anagram

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.

 

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

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

 

Constraints:

  • 1 <= s.length, t.length <= 5 * 104
  • s and t consist of lowercase English letters.

Code Examples

#1 Code Example with C Programming

Code - C Programming


bool isAnagram(char* s, char* t) {
    int n1[26] = { 0 }, n2[26] = { 0 };
    int i, j;
    
    while (*s && *t) {
        i = *s - 'a';
        j = *t - 'a';
        n1[i] ++;
        n2[j] ++;
        s ++;
        t ++;
    }
    
    if (*s || *t) return false;
    
    for (i = 0; i  <  26; i ++) {
        if (n1[i] != n2[i]) return false;
    }
    
    return true;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "anagram", t = "nagaram"

Output

x
+
cmd
true

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public boolean isAnagram(String s, String t) {
    int[] counter = new int[26];
    for (char c : s.toCharArray()) {
      counter[c - 'a']++;
    }
    for (char c : t.toCharArray()) {
      counter[c - 'a']--;
    }
    for (int i = 0; i  <  26; i++) {
      if (counter[i] != 0) {
        return false;
      }
    }
    return true;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "anagram", t = "nagaram"

Output

x
+
cmd
true

#3 Code Example with Javascript Programming

Code - Javascript Programming


const isAnagram = function(s, t) {
  if (s.length !== t.length) return false;
  const sh = strHash(s);
  const th = strHash(t);
  for (let key in sh) {
    if (sh.hasOwnProperty(key) && sh[key] !== th[key]) {
      return false;
    }
  }
  return true;
};

function strHash(str) {
  let res = {};
  for (let i = 0; i  <  str.length; i++) {
    if (res.hasOwnProperty(str[i])) {
      res[str[i]] += 1;
    } else {
      res[str[i]] = 1;
    }
  }
  return res;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "rat", t = "car"

Output

x
+
cmd
false

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        #return sum([ord(i) for i in s])==sum([ord(j) for j in t]) and set(s)==set(t)
        return sorted(s)==sorted(t)
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "rat", t = "car"

Output

x
+
cmd
false

#5 Code Example with C# Programming

Code - C# Programming


using System.Linq;

namespace LeetCode
{
    public class _0242_ValidAnagram
    {
        public bool IsAnagram(string s, string t)
        {
            if (s.Length != t.Length) return false;

            var sCount = new int[26];
            foreach (var ch in s)
                sCount[ch - 'a']++;

            var tCount = new int[26];
            foreach (var ch in t)
                tCount[ch - 'a']++;

            return sCount.SequenceEqual(tCount);
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
s = "anagram", t = "nagaram"

Output

x
+
cmd
true
Advertisements

Demonstration


Previous
#241 Leetcode Different Ways to Add Parentheses Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#257 Leetcode Binary Tree Paths Solution in C, C++, Java, JavaScript, Python, C# Leetcode