Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Valid Anagram LeetCode Solution – Queslers

Problem – 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


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

Valid Anagram LeetCode Solution in Java

public class Solution {
    public boolean isAnagram(String s, String t) {
        int[] alphabet = new int[26];
        for (int i = 0; i < s.length(); i++) alphabet[s.charAt(i) - 'a']++;
        for (int i = 0; i < t.length(); i++) alphabet[t.charAt(i) - 'a']--;
        for (int i : alphabet) if (i != 0) return false;
        return true;

Valid Anagram LeetCode Solution in C++

class Solution {
    bool isAnagram(string s, string t) { 
        sort(s.begin(), s.end());
        sort(t.begin(), t.end());
        return s == t; 

Valid Anagram LeetCode Solution in Python

class Solution:
    def isAnagram(self, s, t):
        return Counter(s) == Counter(t)
Valid Anagram LeetCode Solution Review:

In our experience, we suggest you solve this Valid Anagram LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Valid Anagram LeetCode Solution

Find on LeetCode


I hope this Valid Anagram LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *