Created
May 14, 2022 11:12
-
-
Save bhavjot/ed3c92023d59fbfe52b4a3df568d5a11 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool isAnagram(string a, string b) | |
{ | |
if(a.Length != b.Length){ | |
return false; | |
} | |
//Your code here | |
var aDict = new Dictionary<char,int>(); | |
var bDict = new Dictionary<char,int>(); | |
var aArr = a.ToCharArray(); | |
var bArr = b.ToCharArray(); | |
for(int i=0;i<aArr.Length;i++){ | |
if(aDict.ContainsKey(aArr[i])){ | |
aDict[aArr[i]]++; | |
} | |
else{ | |
aDict.Add(aArr[i],1); | |
} | |
if(bDict.ContainsKey(bArr[i])){ | |
bDict[bArr[i]]++; | |
} | |
else{ | |
bDict.Add(bArr[i],1); | |
} | |
} | |
foreach (char key in aDict.Keys){ | |
if(bDict.ContainsKey(key)){ | |
if(!(aDict[key]==bDict[key])) | |
return false; | |
} | |
else{ | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment