Skip to content

Commit

Permalink
feat: leetcode 205
Browse files Browse the repository at this point in the history
  • Loading branch information
王洋 committed Jan 15, 2024
1 parent a93eabd commit fb17be4
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align="center">
<!-- TOPICS COUNT START -->
<img src="https://img.shields.io/badge/-进度:179-green" alt="进度:179">
<img src="https://img.shields.io/badge/-进度:180-green" alt="进度:180">
<!-- TOPICS COUNT END -->
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>
Expand Down
6 changes: 6 additions & 0 deletions assets/data/categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,12 @@
"path": "./problemset/excel-sheet-column-number/README.md",
"difficulty": "简单"
},
{
"id": "205",
"title": "同构字符串",
"path": "./problemset/isomorphic-strings/README.md",
"difficulty": "简单"
},
{
"id": "541",
"title": "反转字符串 II",
Expand Down
10 changes: 10 additions & 0 deletions assets/data/topics.json
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,16 @@
"url": "https://leetcode.cn/problems/number-of-1-bits/",
"path": "./problemset/number-of-1-bits/README.md"
},
{
"id": "205",
"title": {
"cn": "同构字符串",
"en": "isomorphic-strings"
},
"difficulty": "简单",
"url": "https://leetcode.cn/problems/isomorphic-strings",
"path": "./problemset/isomorphic-strings/README.md"
},
{
"id": "206",
"title": {
Expand Down
1 change: 1 addition & 0 deletions assets/docs/CATEGORIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
| 165. [比较版本号](../../problemset/compare-version-numbers/README.md) | 中等 |
| 168. [Excel表列名称](../../problemset/excel-sheet-column-title/README.md) | 简单 |
| 171. [Excel 表列序号](../../problemset/excel-sheet-column-number/README.md) | 简单 |
| 205. [同构字符串](../../problemset/isomorphic-strings/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) | 中等 |
Expand Down
2 changes: 2 additions & 0 deletions assets/docs/TOPICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@

[191. 位1的个数](../../problemset/number-of-1-bits/README.md)

[205. 同构字符串](../../problemset/isomorphic-strings/README.md)

[206. 反转链表](../../problemset/reverse-linked-list/README.md)

[209. 长度最小的子数组](../../problemset/minimum-size-subarray-sum/README.md)
Expand Down
129 changes: 129 additions & 0 deletions problemset/isomorphic-strings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# 同构字符串

> 难度:简单
>
> https://leetcode.cn/problems/isomorphic-strings
## 题目

给定两个字符串 `s``t` ,判断它们是否是同构的。

如果 `s` 中的字符可以按某种映射关系替换得到 `t` ,那么这两个字符串是同构的。

每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。

### 示例 1:
```typescript
输入:s = "egg", t = "add"
输出:true
```

### 示例 2:
```typescript
输入:s = "foo", t = "bar"
输出:false
```

### 示例 3:
```typescript
输入:s = "paper", t = "title"
输出:true
```

## 题解

### typescript1
```typescript
export function isIsomorphic(s: string, t: string): boolean {
const len = s.length;
const mapS: any = {};
const orderS = [];
const mapT: any = {};
const orderT = [];

for (let i = 0; i < len; i++) {
if (mapS[s[i]]) {
mapS[s[i]].push(i);
} else {
mapS[s[i]] = [i]
orderS.push(s[i])
}

if (mapT[t[i]]) {
mapT[t[i]].push(i);
} else {
mapT[t[i]] = [i]
orderT.push(t[i])
}
}

let ans = true;
if (orderS.length !== orderT.length) {
ans = false
} else {
// let _s = ''; _t = '';
for (let i = 0; i < orderS.length; i++) {
// _s =
if (mapS[orderS[i]].join(',') !== mapT[orderT[i]].join(',')) {
ans = false;
break;
}
}
}

return ans
}
```

### typescript2
```typescript
export function isIsomorphic1(s: string, t: string): boolean {
function fn(s: string, t: string): boolean {
let ans = true;
for (let i = 0; i < s.length - 1; i++) {
for (let j = i + 1; j < s.length; j++) {
if (s[i] === s[j]) {
if (t[i] !== t[j]) {
ans = false
break;
}
}
}
if (!ans) {
break;
}
}
return ans;
}
return fn(s, t) && fn(t, s);
}
```

### python
```python
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
ans = 1
if(len(s) != len(t)):
return ans != 0
lenS = len(s)
arrS = range(lenS)
for i in arrS[0:lenS-1]:
for j in arrS[i+1: lenS]:
if s[i]==s[j]:
if t[i]!=t[j]:
ans = 0
break
if t[i]==t[j]:
if s[i]!=s[j]:
ans = 0
break
if ans == 0:
break
return ans == 1

# s = Solution()
# print(s.isIsomorphic('egg','add'))
# print(s.isIsomorphic('foo','bar'))
# print(s.isIsomorphic('paper','title'))
```
25 changes: 25 additions & 0 deletions problemset/isomorphic-strings/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
ans = 1
if(len(s) != len(t)):
return ans != 0
lenS = len(s)
arrS = range(lenS)
for i in arrS[0:lenS-1]:
for j in arrS[i+1: lenS]:
if s[i]==s[j]:
if t[i]!=t[j]:
ans = 0
break
if t[i]==t[j]:
if s[i]!=s[j]:
ans = 0
break
if ans == 0:
break
return ans == 1

s = Solution()
print(s.isIsomorphic('egg','add'))
print(s.isIsomorphic('foo','bar'))
print(s.isIsomorphic('paper','title'))
19 changes: 19 additions & 0 deletions problemset/isomorphic-strings/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest'
import { isIsomorphic } from '.'

describe('同构字符串', () => {
describe('typescript', () => {
testCase(isIsomorphic)
})
})

function testCase(fn: (s: string, t: string) => boolean) {
it.each([
// test cases
['egg', 'add', true],
['foo', 'bar', false],
['paper', 'title', true],
])('示例%#', (s, t, expected) => {
expect(fn(s, t)).toBe(expected)
})
}
60 changes: 60 additions & 0 deletions problemset/isomorphic-strings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export function isIsomorphic(s: string, t: string): boolean {
const len = s.length;
const mapS: any = {};
const orderS = [];
const mapT: any = {};
const orderT = [];

for (let i = 0; i < len; i++) {
if (mapS[s[i]]) {
mapS[s[i]].push(i);
} else {
mapS[s[i]] = [i]
orderS.push(s[i])
}

if (mapT[t[i]]) {
mapT[t[i]].push(i);
} else {
mapT[t[i]] = [i]
orderT.push(t[i])
}
}

let ans = true;
if (orderS.length !== orderT.length) {
ans = false
} else {
// let _s = ''; _t = '';
for (let i = 0; i < orderS.length; i++) {
// _s =
if (mapS[orderS[i]].join(',') !== mapT[orderT[i]].join(',')) {
ans = false;
break;
}
}
}

return ans
}

export function isIsomorphic1(s: string, t: string): boolean {
function fn(s: string, t: string): boolean {
let ans = true;
for (let i = 0; i < s.length - 1; i++) {
for (let j = i + 1; j < s.length; j++) {
if (s[i] === s[j]) {
if (t[i] !== t[j]) {
ans = false
break;
}
}
}
if (!ans) {
break;
}
}
return ans;
}
return fn(s, t) && fn(t, s);
}

0 comments on commit fb17be4

Please sign in to comment.