-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
王洋
committed
Jan 18, 2024
1 parent
b39b7c0
commit 2e3068c
Showing
9 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# 有效的字母异位词 | ||
|
||
> 难度:简单 | ||
> | ||
> https://leetcode.cn/problems/valid-anagram | ||
## 题目 | ||
|
||
给定两个字符串 `s` 和 `t` ,编写一个函数来判断 `t` 是否是 `s` 的字母异位词。 | ||
|
||
注意:若 `s` 和 `t` 中每个字符出现的次数都相同,则称 `s` 和 `t` 互为字母异位词。 | ||
|
||
|
||
### 示例 1: | ||
```javascript | ||
输入: s = "anagram", t = "nagaram" | ||
输出: true | ||
``` | ||
|
||
### 示例 2: | ||
```javascript | ||
输入: s = "rat", t = "car" | ||
输出: false | ||
``` | ||
|
||
## 题解 | ||
|
||
### TS | ||
|
||
```javascript | ||
export function isAnagram(s: string, t: string): boolean { | ||
if (s.length !== t.length) { | ||
return false | ||
} | ||
const mapS = new Map<string, number>(); | ||
const mapT = new Map<string, number>(); | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
mapS.set(s[i], (mapS.get(s[i]) || 0) + 1); | ||
mapT.set(t[i], (mapT.get(t[i]) || 0) + 1); | ||
} | ||
|
||
if (mapS.size !== mapT.size) { | ||
return false; | ||
} | ||
|
||
let ans = true; | ||
|
||
for (const key of mapS.keys()) { | ||
if (mapS.get(key) !== mapT.get(key)) { | ||
ans = false; | ||
break; | ||
} | ||
} | ||
return ans | ||
} | ||
|
||
// copilot 生成 | ||
export function isAnagram1(s: string, t: string): boolean { | ||
if (s.length !== t.length) { | ||
return false; | ||
} | ||
const mapS = new Map<string, number>(); | ||
const mapT = new Map<string, number>(); | ||
|
||
// Count the frequency of each character in string s | ||
for (const char of s) { | ||
mapS.set(char, (mapS.get(char) || 0) + 1); | ||
} | ||
for (const char of t) { | ||
mapT.set(char, (mapT.get(char) || 0) + 1); | ||
} | ||
|
||
// Count the frequency of each character in string t | ||
for (const [char, count] of mapS) { | ||
if (mapT.get(char) !== count) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
``` | ||
|
||
### Python | ||
|
||
```python | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { isAnagram } from '.' | ||
|
||
describe('有效的字母异位词', () => { | ||
describe('TS', () => { | ||
testCase(isAnagram) | ||
}) | ||
}) | ||
|
||
function testCase(fn: (s: string, t: string) => boolean) { | ||
it.each([ | ||
// test cases | ||
['anagram', 'anagram', true], | ||
['rat', 'car', false], | ||
])('示例%#', (s, t, expected) => { | ||
expect(fn(s, t)).toBe(expected) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
export function isAnagram(s: string, t: string): boolean { | ||
if (s.length !== t.length) { | ||
return false | ||
} | ||
const mapS = new Map<string, number>(); | ||
const mapT = new Map<string, number>(); | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
mapS.set(s[i], (mapS.get(s[i]) || 0) + 1); | ||
mapT.set(t[i], (mapT.get(t[i]) || 0) + 1); | ||
} | ||
|
||
if (mapS.size !== mapT.size) { | ||
return false; | ||
} | ||
|
||
let ans = true; | ||
|
||
for (const key of mapS.keys()) { | ||
if (mapS.get(key) !== mapT.get(key)) { | ||
ans = false; | ||
break; | ||
} | ||
} | ||
return ans | ||
} | ||
|
||
// copilot 生成 | ||
export function isAnagram1(s: string, t: string): boolean { | ||
if (s.length !== t.length) { | ||
return false; | ||
} | ||
const mapS = new Map<string, number>(); | ||
const mapT = new Map<string, number>(); | ||
|
||
// Count the frequency of each character in string s | ||
for (const char of s) { | ||
mapS.set(char, (mapS.get(char) || 0) + 1); | ||
} | ||
for (const char of t) { | ||
mapT.set(char, (mapT.get(char) || 0) + 1); | ||
} | ||
|
||
// Count the frequency of each character in string t | ||
for (const [char, count] of mapS) { | ||
if (mapT.get(char) !== count) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters