# Blogs Hub

### by Sumit Chourasia | Sep 28, 2020 | Category :coding | Tags : algorithmडेटा-संरचनाआसानhash-tableleetcode

#### Valid Anagram - Hash Table - Easy - LeetCode - मिनी टीवी

Given two strings s and t , write a function to determine if t is an anagram of s.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true
Example 2:

Input: s = "rat", t = "car"
Output: false
Note:
You may assume the string contains only lowercase alphabets.

What if the inputs contain unicode characters? How would you adapt your solution to such case?

public class Solution {
public bool IsAnagram(string s, string t) {
if(s.Length != t.Length){
return false;
}
var map1 = new Dictionary<char,int>();
var map2 = new Dictionary<char,int>();
for(int i=0;i<s.Length;i++){
if(map1.ContainsKey(s[i])){
map1[s[i]]++;
}
else{
}

if(map2.ContainsKey(t[i])){
map2[t[i]]++;
}
else{
}
}

foreach(var item in map1){
if(!map2.ContainsKey(item.Key)){
return false;
}

if(item.Value != map2[item.Key]){
return false;
}
}

foreach(var item in map2){
if(!map1.ContainsKey(item.Key)){
return false;
}

if(item.Value != map1[item.Key]){
return false;
}
}

return true;
}
}

Time Complexity: O(n)

Space Complexity: O(n)

Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia