Siblings :
| Date and time note was created | $= dv.current().file.ctime |
| Date and time note was modified | $= dv.current().file.mtime |
Anagrams are words or phrases formed by rearranging the letters of a different word. Example: cat and act.
**Question Link : ** Leetcode-242 Valid Anagram
Question Breakdown
Approaches
Brute force
Code
#include<bits/stdc++.h>
using namespace std;
bool isAnagram(string s, string t) {
if(s.length()!=t.length()){
return false;
}
sort(s.begin(),s.end());
sort(t.begin(),t.end());
for(int i=0;i<s.length();i++){
if(s[i]!=t[i]){
return false;
}
}
return true;
} Time Complexity
Space Complexity
Optimised solution
Code
bool isAnagram(string s, string t) {
int cS[26]={0},tS[26]={0};
if(s.length()!=t.length()){
return false;
}
for(auto&it:s)
cS[it-'a']++;// this calculates the value of it-'a' in ascii integer diff
for(auto&it:t)
tS[it-'a']++;// this calculates the value of it-'a' in ascii integer diff
for(int i=0;i<26;i++){
if(tS[i]!=cS[i]){
return false;
}
}
return true;
}Time Complexity
Space Complexity