From d689012bc028d92df63e25a7a1b99c0396e7f7eb Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Sun, 18 Jul 2021 17:14:06 +0800 Subject: [PATCH] feat: add typescript solution to lc problem: No.0242.Valid Anagram --- solution/0200-0299/0242.Valid Anagram/README.md | 15 +++++++++++++++ .../0200-0299/0242.Valid Anagram/README_EN.md | 15 +++++++++++++++ solution/0200-0299/0242.Valid Anagram/Solution.ts | 10 ++++++++++ 3 files changed, 40 insertions(+) create mode 100644 solution/0200-0299/0242.Valid Anagram/Solution.ts diff --git a/solution/0200-0299/0242.Valid Anagram/README.md b/solution/0200-0299/0242.Valid Anagram/README.md index 1d57d61c10625..f7c24a5638de6 100644 --- a/solution/0200-0299/0242.Valid Anagram/README.md +++ b/solution/0200-0299/0242.Valid Anagram/README.md @@ -80,6 +80,21 @@ class Solution { } ``` +### **TypeScript** + +```ts +function isAnagram(s: string, t: string): boolean { + if (s.length != t.length) return false; + let record = new Array(26).fill(0); + let base = 'a'.charCodeAt(0); + for (let i = 0; i < s.length; ++i) { + ++record[s.charCodeAt(i) - base]; + --record[t.charCodeAt(i) - base]; + } + return record.every(v => v == 0); +}; +``` + ### **C++** ```cpp diff --git a/solution/0200-0299/0242.Valid Anagram/README_EN.md b/solution/0200-0299/0242.Valid Anagram/README_EN.md index ce577765f5cb8..758b506ab1e10 100644 --- a/solution/0200-0299/0242.Valid Anagram/README_EN.md +++ b/solution/0200-0299/0242.Valid Anagram/README_EN.md @@ -72,6 +72,21 @@ class Solution { } ``` +### **TypeScript** + +```ts +function isAnagram(s: string, t: string): boolean { + if (s.length != t.length) return false; + let record = new Array(26).fill(0); + let base = 'a'.charCodeAt(0); + for (let i = 0; i < s.length; ++i) { + ++record[s.charCodeAt(i) - base]; + --record[t.charCodeAt(i) - base]; + } + return record.every(v => v == 0); +}; +``` + ### **C++** ```cpp diff --git a/solution/0200-0299/0242.Valid Anagram/Solution.ts b/solution/0200-0299/0242.Valid Anagram/Solution.ts new file mode 100644 index 0000000000000..326b3bc10697f --- /dev/null +++ b/solution/0200-0299/0242.Valid Anagram/Solution.ts @@ -0,0 +1,10 @@ +function isAnagram(s: string, t: string): boolean { + if (s.length != t.length) return false; + let record = new Array(26).fill(0); + let base = 'a'.charCodeAt(0); + for (let i = 0; i < s.length; ++i) { + ++record[s.charCodeAt(i) - base]; + --record[t.charCodeAt(i) - base]; + } + return record.every(v => v == 0); +}; \ No newline at end of file