DEV Community

VipulDeshmukh95
VipulDeshmukh95

Posted on

My first LeetCode(which I failed, although I had a correct logic in my mind)

LeetCode#242 Anagram Code

class Solution {
public:
bool isAnagram(string s, string t) {

   int n1 = s.length();
   int n2 = t.length();

    if((n1  > 50000) || (n2 > 50000))
    {
        return false;

    }
    if(n1 != n2)
    {
         return false;
    }

    //sorting both strings
    sort(s.begin(), s.end());
    sort(t.begin(), t.end());

    for(int i = 0; i <= n2; i++)
    {
         if(s[i] != s[i])
             return false;
    }

  return true;  
}
Enter fullscreen mode Exit fullscreen mode

};

Top comments (0)