-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidAnagram.cs
More file actions
56 lines (46 loc) · 1.32 KB
/
validAnagram.cs
File metadata and controls
56 lines (46 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// https://leetcode.com/problems/valid-anagram/
// beats 93.43%
public class Solution {
public bool IsAnagram(string s, string t) {
if(s.Length != t.Length){
return false;
}
Dictionary <char, int> sMap = new Dictionary<char, int>();
Dictionary <char, int> tMap = new Dictionary<char, int>();
foreach(char letter in s){
if(sMap.ContainsKey(letter)){
sMap[letter]++;
}else{
sMap[letter] = 1;
}
}
foreach(char letter in t){
if(tMap.ContainsKey(letter)){
tMap[letter]++;
}else{
tMap[letter] = 1;
}
}
foreach(var letter in s){
if(!tMap.ContainsKey(letter) || sMap[letter] != tMap[letter]){
return false;
}
}
return true;
}
}
// beats 25.47%
public class Solution {
public bool IsAnagram(string s, string t) {
if (s.Length != t.Length) {
return false;
}
char[] sArray = s.ToCharArray();
char[] tArray = t.ToCharArray();
Array.Sort(sArray);
Array.Sort(tArray);
string sortedS = new string(sArray);
string sortedT = new string(tArray);
return sortedS.Equals(sortedT);
}
}