-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
36 lines (32 loc) · 738 Bytes
/
Solution.cs
File metadata and controls
36 lines (32 loc) · 738 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
32
33
34
35
36
public class Solution
{
public IList<string> RemoveAnagrams(string[] words)
{
int n = words.Length;
List<string> ans = [];
ans.Add(words[0]);
for (int i = 1; i < n; i++)
{
if (IsAnagram(words[i], words[i - 1])) continue;
ans.Add(words[i]);
}
return ans;
}
bool IsAnagram(string word1, string word2)
{
int[] freq = new int[26];
foreach (char ch in word1)
{
freq[ch - 'a']++;
}
foreach (char ch in word2)
{
freq[ch - 'a']--;
}
for (int i = 0; i < 26; i++)
{
if (freq[i] != 0) return false;
}
return true;
}
}