Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

Leetcode Valid Anagram problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Valid Anagram problem solution we have given two strings s and t, return true if t is an anagram of s, and false otherwise.

Leetcode Valid Anagram problem solution

Problem solution in Python.

from collections import Counter
class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        return Counter(s) == Counter(t)

Problem solution in Java.

public boolean isAnagram(String s, String t) {
    int len1=s.length(),len2=t.length();
    if(s==null||t==null||len1!=len2)
      return false;
      HashMap<Character,Integer> map=new HashMap<Character,Integer>();
      for(int i=0;i<len1;i++){
          char c=s.charAt(i);
          if(map.get(c)==null)
            map.put(c,1);
          else
            map.put(c,map.get(c)+1);
      }
      
      for(int i=0;i<len2;i++){
          char c=t.charAt(i);
          if(map.get(c)==null)
            return false;
          else{
            int num=map.get(c);
            if(num==0)
              return false;
            else
              map.put(c,map.get(c)-1);
          }
      }
      
     return true;
}

Problem solution in C++.

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.size()!=t.size()) return false;
        int count[26]={0};
        for(int i=0;i<s.size();i++) 
            count[s[i]-'a']++;
        for(int i=0;i<s.size();i++){
            count[t[i]-'a']--;
            if(count[t[i]-'a']<0) return false;
        }
        return true;
    }
};

Problem solution in C.

bool isAnagram(char * s, char * t){ 
    unsigned long long s1 = 0, s2 = 0, p1 = 1, p2 = 1;  
    for (; *s && *t ; s1 += *s, s2 += *t, p1 *= *s++, p2 *= *t++);
    return !*s && !*t && s1 == s2 && p1 == p2;
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

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

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes