Skip to content

Commit 3d8548c

Browse files
committed
Merge pull request #200 from 0xff-dev/master
Add solution and test-cases for problem 205
2 parents b33e63e + e58d90c commit 3d8548c

File tree

3 files changed

+51
-20
lines changed

3 files changed

+51
-20
lines changed

leetcode/201-300/0205.Isomorphic-Strings/README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
# [205.Isomorphic Strings][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
53

64
## Description
5+
Given two strings `s` and `t`, determine if they are isomorphic.
6+
7+
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
8+
9+
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: s = "egg", t = "add"
15+
Output: true
1316
```
1417

15-
## 题意
16-
> ...
18+
**Example 2:**
19+
20+
```
21+
Input: s = "foo", t = "bar"
22+
Output: false
23+
```
1724

18-
## 题解
25+
**Example 3:**
1926

20-
### 思路1
21-
> ...
22-
Isomorphic Strings
23-
```go
27+
```
28+
Input: s = "paper", t = "title"
29+
Output: true
2430
```
2531

2632

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,27 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string, t string) bool {
4+
ls, lt := len(s), len(t)
5+
if ls != lt {
6+
return false
7+
}
8+
9+
mapping := make(map[byte]byte)
10+
reverseMapping := make(map[byte]struct{})
11+
for idx := 0; idx < ls; idx++ {
12+
if v, ok := mapping[s[idx]]; ok {
13+
if v != t[idx] {
14+
return false
15+
}
16+
continue
17+
}
18+
if _, ok := reverseMapping[t[idx]]; ok {
19+
return false
20+
}
21+
22+
mapping[s[idx]] = t[idx]
23+
reverseMapping[t[idx]] = struct{}{}
24+
}
25+
26+
return true
527
}

leetcode/201-300/0205.Isomorphic-Strings/Solution_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
s, t string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "egg", "add", true},
17+
{"TestCase2", "foo", "bar", false},
18+
{"TestCase3", "bar", "foo", false},
19+
{"TestCase4", "paper", "title", true},
20+
{"TestCase5", "title", "paper", true},
21+
{"TestCase6", "abcd", "efgh", true},
1922
}
2023

2124
// 开始测试
2225
for i, c := range cases {
2326
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
27+
got := Solution(c.s, c.t)
2528
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
29+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
30+
c.expect, got, c.s, c.t)
2831
}
2932
})
3033
}

0 commit comments

Comments
 (0)