mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 02:53:31 +08:00
Merge pull request #2326 from FarmerChillax/master
Update 0242.有效的字母异位词,添加第二种Go解法
This commit is contained in:
@ -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:
|
### JavaScript:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
Reference in New Issue
Block a user