I need help with anagram problem

Here is the leetcode problem link : valid-anagram

It is giving WA in
“a”
“b”

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

class Solution {
public:
bool isAnagram(string s, string t)
{
if(s.length() != t.length())
return false;
int count[256]={};

for(int i=0;i<s.length();i++) //changed from s.length()-1 to s.length()
    count[s[i]-'a']++;

for(int i=0;i<t.length();i++)  //changed from t.length()-1 to t.length()
    count[t[i]-'a']--;


for(int i=0;i<256;i++)
    if(count[i] != 0)
        return false;

 return true;

}
};

And now it will give right answers :grinning: :grinning:

Why are you traversing till length -1 ?

thanks a lot…

Oops, my bad, I really don’t know why I was traversing till length-1 :joy: