-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidAnagram.cs
More file actions
31 lines (24 loc) · 808 Bytes
/
ValidAnagram.cs
File metadata and controls
31 lines (24 loc) · 808 Bytes
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
namespace Solutions.Problems;
public class ValidAnagramSolution
{
public bool IsAnagramSorting(string s, string t)
{
if (s.Length != t.Length)
return false;
string sortedS = string.Concat(s.OrderBy(s => s));
string sortedT = string.Concat(t.OrderBy(t => t));
return sortedS == sortedT;
}
public bool IsAnagramHashMap(string s, string t)
{
if (s.Length != t.Length)
return false;
var hashMap = new Dictionary<char, int>();
for (int index = 0; index < s.Length; index++)
{
hashMap[s[index]] = hashMap.GetValueOrDefault(s[index], 0) + 1;
hashMap[t[index]] = hashMap.GetValueOrDefault(t[index], 0) - 1;
}
return !hashMap.Values.Any(v => v != 0);
}
}