Leetcode 242 Valid Anagram Solution in c++ | Hindi Coding Community

0

  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



class Solution {
public:
    bool isAnagram(string s, string t) {
     
        if(s.length() != t.length()) return false;
     
        vector<int> counter1(26,0), counter2(26,0);
   
        for(int idx = 0; idx < s.length(); idx++) {
            counter1[s[idx]-'a']++;
            counter2[t[idx]-'a']++;
        }
       
        for(int idx = 0; idx < 26; idx++) {
           
            if(counter1[idx] != counter2[idx]) return false;
        }
        return true;      
    }
};

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !