-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2273-FindResultantArrayAfterRemovingAnagrams.go
More file actions
81 lines (70 loc) · 3.47 KB
/
2273-FindResultantArrayAfterRemovingAnagrams.go
File metadata and controls
81 lines (70 loc) · 3.47 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
// 2273. Find Resultant Array After Removing Anagrams
// You are given a 0-indexed string array words, where words[i] consists of lowercase English letters.
// In one operation, select any index i such that 0 < i < words.length and words[i - 1] and words[i] are anagrams, and delete words[i] from words.
// Keep performing this operation as long as you can select an index that satisfies the conditions.
// Return words after performing all operations.
// It can be shown that selecting the indices for each operation in any arbitrary order will lead to the same result.
// An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase using all the original letters exactly once.
// For example, "dacb" is an anagram of "abdc".
// Example 1:
// Input: words = ["abba","baba","bbaa","cd","cd"]
// Output: ["abba","cd"]
// Explanation:
// One of the ways we can obtain the resultant array is by using the following operations:
// - Since words[2] = "bbaa" and words[1] = "baba" are anagrams, we choose index 2 and delete words[2].
// Now words = ["abba","baba","cd","cd"].
// - Since words[1] = "baba" and words[0] = "abba" are anagrams, we choose index 1 and delete words[1].
// Now words = ["abba","cd","cd"].
// - Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2].
// Now words = ["abba","cd"].
// We can no longer perform any operations, so ["abba","cd"] is the final answer.
// Example 2:
// Input: words = ["a","b","c","d","e"]
// Output: ["a","b","c","d","e"]
// Explanation:
// No two adjacent strings in words are anagrams of each other, so no operations are performed.
// Constraints:
// 1 <= words.length <= 100
// 1 <= words[i].length <= 10
// words[i] consists of lowercase English letters.
import "fmt"
func removeAnagrams(words []string) []string {
res, prev := []string{}, make([]int, 26)
for _, word := range words {
cur := make([]int, 26)
for i := 0; i < len(word); i++ {
cur[word[i] - 'a']++
}
for i := 0; i < 26; i++ {
if cur[i] != prev[i] {
res = append(res, word)
prev = cur
break
}
}
}
return res
}
func main() {
// Example 1:
// Input: words = ["abba","baba","bbaa","cd","cd"]
// Output: ["abba","cd"]
// Explanation:
// One of the ways we can obtain the resultant array is by using the following operations:
// - Since words[2] = "bbaa" and words[1] = "baba" are anagrams, we choose index 2 and delete words[2].
// Now words = ["abba","baba","cd","cd"].
// - Since words[1] = "baba" and words[0] = "abba" are anagrams, we choose index 1 and delete words[1].
// Now words = ["abba","cd","cd"].
// - Since words[2] = "cd" and words[1] = "cd" are anagrams, we choose index 2 and delete words[2].
// Now words = ["abba","cd"].
// We can no longer perform any operations, so ["abba","cd"] is the final answer.
fmt.Println(removeAnagrams([]string{"abba","baba","bbaa","cd","cd"})) // ["abba","cd"]
// Example 2:
// Input: words = ["a","b","c","d","e"]
// Output: ["a","b","c","d","e"]
// Explanation:
// No two adjacent strings in words are anagrams of each other, so no operations are performed.
fmt.Println(removeAnagrams([]string{"a","b","c","d","e"})) // ["a","b","c","d","e"]
fmt.Println(removeAnagrams([]string{"bluefrog", "freewu","leetcode"})) // ["bluefrog","freewu","leetcode"]
}