diff --git a/README.md b/README.md index 8e698e5..a0bb0a8 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@

-进度:180 +进度:181 题库目录 题库分类 diff --git a/assets/data/categories.json b/assets/data/categories.json index 7c8d4c6..1b0ba92 100644 --- a/assets/data/categories.json +++ b/assets/data/categories.json @@ -145,6 +145,12 @@ "path": "./problemset/contains-duplicate/README.md", "difficulty": "简单" }, + { + "id": "242", + "title": "有效的字母异位词", + "path": "./problemset/valid-anagram/README.md", + "difficulty": "简单" + }, { "id": "349", "title": "两个数组的交集", @@ -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", diff --git a/assets/data/topics.json b/assets/data/topics.json index fe97f74..697daf0 100644 --- a/assets/data/topics.json +++ b/assets/data/topics.json @@ -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": { diff --git a/assets/docs/CATEGORIES.md b/assets/docs/CATEGORIES.md index b3faccd..c63de1e 100644 --- a/assets/docs/CATEGORIES.md +++ b/assets/docs/CATEGORIES.md @@ -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) | 中等 | @@ -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) | 中等 | diff --git a/assets/docs/TOPICS.md b/assets/docs/TOPICS.md index 6bd3616..c585e45 100644 --- a/assets/docs/TOPICS.md +++ b/assets/docs/TOPICS.md @@ -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) diff --git a/problemset/valid-anagram/README.md b/problemset/valid-anagram/README.md new file mode 100644 index 0000000..a454105 --- /dev/null +++ b/problemset/valid-anagram/README.md @@ -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(); + const mapT = new Map(); + + 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(); + const mapT = new Map(); + + // 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 + +``` diff --git a/problemset/valid-anagram/index.spec.ts b/problemset/valid-anagram/index.spec.ts new file mode 100644 index 0000000..9efce96 --- /dev/null +++ b/problemset/valid-anagram/index.spec.ts @@ -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) + }) +} diff --git a/problemset/valid-anagram/index.ts b/problemset/valid-anagram/index.ts new file mode 100644 index 0000000..e16a0c3 --- /dev/null +++ b/problemset/valid-anagram/index.ts @@ -0,0 +1,52 @@ +export function isAnagram(s: string, t: string): boolean { + if (s.length !== t.length) { + return false + } + const mapS = new Map(); + const mapT = new Map(); + + 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(); + const mapT = new Map(); + + // 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; +} diff --git a/scripts/create.ts b/scripts/create.ts index 05b239d..171efdd 100644 --- a/scripts/create.ts +++ b/scripts/create.ts @@ -15,7 +15,7 @@ interface InquirerAnswers { // main (async() => { // git pull - await commandAction('git', ['pull']) + // await commandAction('git', ['pull']) // clear terminal clear()