Merge pull request #2326 from FarmerChillax/master

Update 0242.有效的字母异位词,添加第二种Go解法
This commit is contained in:
程序员Carl
2023-11-08 09:45:42 +08:00
committed by GitHub

View File

@ -181,6 +181,31 @@ func isAnagram(s string, t string) bool {
}
```
Go 写法二(只对字符串遍历一次)
```go
func isAnagram(s string, t string) bool {
if len(s) != len(t) {
return false
}
records := [26]int{}
for index := 0; index < len(s); index++ {
if s[index] == t[index] {
continue
}
sCharIndex := s[index] - 'a'
records[sCharIndex]++
tCharIndex := t[index] - 'a'
records[tCharIndex]--
}
for _, record := range records {
if record != 0 {
return false
}
}
return true
}
```
### JavaScript:
```js