1657. 确定两个字符串是否接近

1657. 确定两个字符串是否接近

解法一: 哈希计数

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
func closeStrings(word1 string, word2 string) bool {
n := len(word1)
if n != len(word2) {
return false
}
counter := [2][26]int{}
flags := [2]int{}
for i := 0; i < n; i++ {
counter[0][word1[i] - 'a']++
counter[1][word2[i] - 'a']++
flags[0] |= 1 << int(word1[i] - 'a')
flags[1] |= 1 << int(word2[i] - 'a')
}
if flags[0] != flags[1] {
return false
}
sort.Ints(counter[0][:])
sort.Ints(counter[1][:])
return counter[0] == counter[1]
}
作者

wuhunyu

发布于

2023-11-30

更新于

2023-11-30

许可协议