diff --git a/solution/0500-0599/0500.Keyboard Row/README.md b/solution/0500-0599/0500.Keyboard Row/README.md index 4d603b5368803..5e387bbe3a906 100644 --- a/solution/0500-0599/0500.Keyboard Row/README.md +++ b/solution/0500-0599/0500.Keyboard Row/README.md @@ -194,6 +194,30 @@ public class Solution { } ``` +### **TypeScript** + +```ts +function findWords(words: string[]): string[] { + const s = '12210111011122000010020202'; + const ans: string[] = []; + for (const w of words) { + const t = w.toLowerCase(); + const x = s[t.charCodeAt(0) - 'a'.charCodeAt(0)]; + let ok = true; + for (const c of t) { + if (s[c.charCodeAt(0) - 'a'.charCodeAt(0)] !== x) { + ok = false; + break; + } + } + if (ok) { + ans.push(w); + } + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/0500-0599/0500.Keyboard Row/README_EN.md b/solution/0500-0599/0500.Keyboard Row/README_EN.md index 655acb207d6f0..0bf2ab2bcee0a 100644 --- a/solution/0500-0599/0500.Keyboard Row/README_EN.md +++ b/solution/0500-0599/0500.Keyboard Row/README_EN.md @@ -176,6 +176,30 @@ public class Solution { } ``` +### **TypeScript** + +```ts +function findWords(words: string[]): string[] { + const s = '12210111011122000010020202'; + const ans: string[] = []; + for (const w of words) { + const t = w.toLowerCase(); + const x = s[t.charCodeAt(0) - 'a'.charCodeAt(0)]; + let ok = true; + for (const c of t) { + if (s[c.charCodeAt(0) - 'a'.charCodeAt(0)] !== x) { + ok = false; + break; + } + } + if (ok) { + ans.push(w); + } + } + return ans; +} +``` + ### **...** ``` diff --git a/solution/0500-0599/0500.Keyboard Row/Solution.ts b/solution/0500-0599/0500.Keyboard Row/Solution.ts new file mode 100644 index 0000000000000..756cfc9597109 --- /dev/null +++ b/solution/0500-0599/0500.Keyboard Row/Solution.ts @@ -0,0 +1,19 @@ +function findWords(words: string[]): string[] { + const s = '12210111011122000010020202'; + const ans: string[] = []; + for (const w of words) { + const t = w.toLowerCase(); + const x = s[t.charCodeAt(0) - 'a'.charCodeAt(0)]; + let ok = true; + for (const c of t) { + if (s[c.charCodeAt(0) - 'a'.charCodeAt(0)] !== x) { + ok = false; + break; + } + } + if (ok) { + ans.push(w); + } + } + return ans; +}