From 690d4c8e2d14a079d70a7823b2f6c9e07b7ab79d Mon Sep 17 00:00:00 2001 From: rain84 Date: Thu, 30 May 2024 23:52:21 +0300 Subject: [PATCH 1/4] feat: add TS solution to lc problem No.290 --- .../0200-0299/0290.Word Pattern/README_EN.md | 29 +++++++++++++++++++ .../0200-0299/0290.Word Pattern/Solution2.ts | 14 +++++++++ 2 files changed, 43 insertions(+) create mode 100644 solution/0200-0299/0290.Word Pattern/Solution2.ts diff --git a/solution/0200-0299/0290.Word Pattern/README_EN.md b/solution/0200-0299/0290.Word Pattern/README_EN.md index b04624d9d6ea3..ddfed4ecce5b7 100644 --- a/solution/0200-0299/0290.Word Pattern/README_EN.md +++ b/solution/0200-0299/0290.Word Pattern/README_EN.md @@ -262,4 +262,33 @@ public class Solution { + + +### Solution 2 + + + +#### TypeScript + +```ts +function wordPattern(pattern: string, s: string): boolean { + const hash: Record = Object.create(null); + const arr = s.split(/\s+/); + + if (pattern.length !== arr.length || new Set(pattern).size !== new Set(arr).size) return false; + + for (let i = 0; i < pattern.length; i++) { + hash[pattern[i]] ??= arr[i]; + + if (hash[pattern[i]] !== arr[i]) return false; + } + + return true; +} +``` + + + + + diff --git a/solution/0200-0299/0290.Word Pattern/Solution2.ts b/solution/0200-0299/0290.Word Pattern/Solution2.ts new file mode 100644 index 0000000000000..326ddc243bc26 --- /dev/null +++ b/solution/0200-0299/0290.Word Pattern/Solution2.ts @@ -0,0 +1,14 @@ +function wordPattern(pattern: string, s: string): boolean { + const hash: Record = Object.create(null); + const arr = s.split(/\s+/); + + if (pattern.length !== arr.length || new Set(pattern).size !== new Set(arr).size) return false; + + for (let i = 0; i < pattern.length; i++) { + hash[pattern[i]] ??= arr[i]; + + if (hash[pattern[i]] !== arr[i]) return false; + } + + return true; +} From 2a6a6b097d74641c63bf27e015ef091c789c9ae8 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 31 May 2024 09:48:49 +0800 Subject: [PATCH 2/4] Update README_EN.md --- solution/0200-0299/0290.Word Pattern/README_EN.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/solution/0200-0299/0290.Word Pattern/README_EN.md b/solution/0200-0299/0290.Word Pattern/README_EN.md index ddfed4ecce5b7..2a8f2bd91b3a0 100644 --- a/solution/0200-0299/0290.Word Pattern/README_EN.md +++ b/solution/0200-0299/0290.Word Pattern/README_EN.md @@ -275,12 +275,15 @@ function wordPattern(pattern: string, s: string): boolean { const hash: Record = Object.create(null); const arr = s.split(/\s+/); - if (pattern.length !== arr.length || new Set(pattern).size !== new Set(arr).size) return false; + if (pattern.length !== arr.length || new Set(pattern).size !== new Set(arr).size) { + return false; + } for (let i = 0; i < pattern.length; i++) { hash[pattern[i]] ??= arr[i]; - - if (hash[pattern[i]] !== arr[i]) return false; + if (hash[pattern[i]] !== arr[i]) { + return false; + } } return true; From 9d5b62390613ec9b9b950a771c09d3b9c98467c7 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 31 May 2024 09:48:58 +0800 Subject: [PATCH 3/4] Update Solution2.ts --- solution/0200-0299/0290.Word Pattern/Solution2.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/solution/0200-0299/0290.Word Pattern/Solution2.ts b/solution/0200-0299/0290.Word Pattern/Solution2.ts index 326ddc243bc26..bb610f1a658ad 100644 --- a/solution/0200-0299/0290.Word Pattern/Solution2.ts +++ b/solution/0200-0299/0290.Word Pattern/Solution2.ts @@ -2,12 +2,15 @@ function wordPattern(pattern: string, s: string): boolean { const hash: Record = Object.create(null); const arr = s.split(/\s+/); - if (pattern.length !== arr.length || new Set(pattern).size !== new Set(arr).size) return false; + if (pattern.length !== arr.length || new Set(pattern).size !== new Set(arr).size) { + return false; + } for (let i = 0; i < pattern.length; i++) { hash[pattern[i]] ??= arr[i]; - - if (hash[pattern[i]] !== arr[i]) return false; + if (hash[pattern[i]] !== arr[i]) { + return false; + } } return true; From 50e2cec45572783a48733f38dc46eb22e89f816c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Fri, 31 May 2024 09:50:50 +0800 Subject: [PATCH 4/4] Update README.md --- .../0200-0299/0290.Word Pattern/README.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/solution/0200-0299/0290.Word Pattern/README.md b/solution/0200-0299/0290.Word Pattern/README.md index c36d9f6879a7c..a9736836e3b8b 100644 --- a/solution/0200-0299/0290.Word Pattern/README.md +++ b/solution/0200-0299/0290.Word Pattern/README.md @@ -261,4 +261,36 @@ public class Solution { + + +### 方法二:哈希表的另一种写法 + + + +#### TypeScript + +```ts +function wordPattern(pattern: string, s: string): boolean { + const hash: Record = Object.create(null); + const arr = s.split(/\s+/); + + if (pattern.length !== arr.length || new Set(pattern).size !== new Set(arr).size) { + return false; + } + + for (let i = 0; i < pattern.length; i++) { + hash[pattern[i]] ??= arr[i]; + if (hash[pattern[i]] !== arr[i]) { + return false; + } + } + + return true; +} +``` + + + + +