Skip to content

Commit

Permalink
feat: leetcode 242
Browse files Browse the repository at this point in the history
  • Loading branch information
王洋 committed Jan 18, 2024
1 parent b39b7c0 commit 2e3068c
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align="center">
<!-- TOPICS COUNT START -->
<img src="https://img.shields.io/badge/-进度:180-green" alt="进度:180">
<img src="https://img.shields.io/badge/-进度:181-green" alt="进度:181">
<!-- TOPICS COUNT END -->
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>
Expand Down
12 changes: 12 additions & 0 deletions assets/data/categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@
"path": "./problemset/contains-duplicate/README.md",
"difficulty": "简单"
},
{
"id": "242",
"title": "有效的字母异位词",
"path": "./problemset/valid-anagram/README.md",
"difficulty": "简单"
},
{
"id": "349",
"title": "两个数组的交集",
Expand Down Expand Up @@ -425,6 +431,12 @@
"path": "./problemset/isomorphic-strings/README.md",
"difficulty": "简单"
},
{
"id": "242",
"title": "有效的字母异位词",
"path": "./problemset/valid-anagram/README.md",
"difficulty": "简单"
},
{
"id": "541",
"title": "反转字符串 II",
Expand Down
10 changes: 10 additions & 0 deletions assets/data/topics.json
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,16 @@
"url": "https://leetcode.cn/problems/invert-binary-tree/description/",
"path": "./problemset/description/README.md"
},
{
"id": "242",
"title": {
"cn": "有效的字母异位词",
"en": "valid-anagram"
},
"difficulty": "简单",
"url": "https://leetcode.cn/problems/valid-anagram",
"path": "./problemset/valid-anagram/README.md"
},
{
"id": "273",
"title": {
Expand Down
2 changes: 2 additions & 0 deletions assets/docs/CATEGORIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
| 169. [多数元素](../../problemset/majority-element/README.md) | 简单 |
| 187. [重复的DNA序列](../../problemset/repeated-dna-sequences/README.md) | 中等 |
| 217. [存在重复元素](../../problemset/contains-duplicate/README.md) | 简单 |
| 242. [有效的字母异位词](../../problemset/valid-anagram/README.md) | 简单 |
| 349. [两个数组的交集](../../problemset/intersection-of-two-arrays/README.md) | 简单 |
| 350. [两个数组的交集 II](../../problemset/intersection-of-two-arrays-ii/README.md) | 简单 |
| 1072. [按列翻转得到最大值等行数](../../problemset/flip-columns-for-maximum-number-of-equal-rows/README.md) | 中等 |
Expand Down Expand Up @@ -87,6 +88,7 @@
| 168. [Excel表列名称](../../problemset/excel-sheet-column-title/README.md) | 简单 |
| 171. [Excel 表列序号](../../problemset/excel-sheet-column-number/README.md) | 简单 |
| 205. [同构字符串](../../problemset/isomorphic-strings/README.md) | 简单 |
| 242. [有效的字母异位词](../../problemset/valid-anagram/README.md) | 简单 |
| 541. [反转字符串 II](../../problemset/reverse-string-ii/README.md) | 简单 |
| 557. [反转字符串中的单词 III](../../problemset/reverse-words-in-a-string-iii/README.md) | 简单 |
| 1016. [子串能表示从 1 到 N 数字的二进制串](../../problemset/binary-string-with-substrings-representing-1-to-n/README.md) | 中等 |
Expand Down
2 changes: 2 additions & 0 deletions assets/docs/TOPICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@

[226. 翻转二叉树](../../problemset/description/README.md)

[242. 有效的字母异位词](../../problemset/valid-anagram/README.md)

[273. 整数转换英文表示](../../problemset/integer-to-english-words/README.md)

[278. 第一个错误的版本](../../problemset/first-bad-version/README.md)
Expand Down
90 changes: 90 additions & 0 deletions problemset/valid-anagram/README.md
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

```
18 changes: 18 additions & 0 deletions problemset/valid-anagram/index.spec.ts
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)
})
}
52 changes: 52 additions & 0 deletions problemset/valid-anagram/index.ts
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;
}
2 changes: 1 addition & 1 deletion scripts/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface InquirerAnswers {
// main
(async() => {
// git pull
await commandAction('git', ['pull'])
// await commandAction('git', ['pull'])

// clear terminal
clear()
Expand Down

0 comments on commit 2e3068c

Please sign in to comment.