Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add TS solution to lc problem No.290 #2969

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions solution/0200-0299/0290.Word Pattern/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,36 @@ public class Solution {

<!-- solution:end -->

<!-- solution:start -->

### 方法二:哈希表的另一种写法

<!-- tabs:start -->

#### TypeScript

```ts
function wordPattern(pattern: string, s: string): boolean {
const hash: Record<string, string> = 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
32 changes: 32 additions & 0 deletions solution/0200-0299/0290.Word Pattern/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,36 @@ public class Solution {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### TypeScript

```ts
function wordPattern(pattern: string, s: string): boolean {
const hash: Record<string, string> = 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;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
17 changes: 17 additions & 0 deletions solution/0200-0299/0290.Word Pattern/Solution2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function wordPattern(pattern: string, s: string): boolean {
const hash: Record<string, string> = 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;
}
Loading