Skip to content

Commit a9c4af1

Browse files
committed
add isomorphic strings
1 parent 1efd32d commit a9c4af1

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Run `readme-generator.py`. Here you ≡Go!
2828

2929
| Solved | Easy | Medium | Hard |
3030
| :----: | :--: | :----: | :--: |
31-
|155|85|61|9|
31+
|156|86|61|9|
3232

3333

3434
## Solutions
@@ -121,6 +121,7 @@ Run `readme-generator.py`. Here you ≡Go!
121121
|200|[Number of Islands](https://leetcode.com/problems/number-of-islands)|Medium|[Go](src/number-of-islands/solution.go)|
122122
|202|[Happy Number](https://leetcode.com/problems/happy-number)|Easy|[Go](src/happy-number/solution.go)|
123123
|204|[Count Primes](https://leetcode.com/problems/count-primes)|Easy|[Go](src/count-primes/solution.go)|
124+
|205|[Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings)|Easy|[Go](src/isomorphic-strings/solution.go)|
124125
|206|[Reverse Linked List](https://leetcode.com/problems/reverse-linked-list)|Easy|[Go](src/reverse-linked-list/solution.go)|
125126
|215|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array)|Medium|[Go](src/kth-largest-element-in-an-array/solution.go)|
126127
|217|[Contains Duplicate](https://leetcode.com/problems/contains-duplicate)|Easy|[Go](src/contains-duplicate/solution.go)|

src/isomorphic-strings/solution.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func isIsomorphic(s string, t string) bool {
6+
if len(s) != len(t) {
7+
return false
8+
}
9+
return checkIsomorphic(s, t) && checkIsomorphic(t, s)
10+
}
11+
12+
func checkIsomorphic(s string, t string) bool {
13+
m := make(map[byte]byte)
14+
for i := range s {
15+
if _, ok := m[s[i]]; ok {
16+
if m[s[i]] != t[i] {
17+
return false
18+
}
19+
} else {
20+
m[s[i]] = t[i]
21+
}
22+
}
23+
return true
24+
}
25+
26+
func main() {
27+
s := "ab"
28+
t := "aa"
29+
fmt.Println(isIsomorphic(s, t))
30+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import "testing"
4+
5+
func Test_checkIsomorphic(t *testing.T) {
6+
type args struct {
7+
s string
8+
t string
9+
}
10+
tests := []struct {
11+
name string
12+
args args
13+
want bool
14+
}{
15+
{
16+
"1",
17+
args{
18+
s: "title",
19+
t: "paper",
20+
},
21+
true,
22+
},
23+
}
24+
for _, tt := range tests {
25+
t.Run(tt.name, func(t *testing.T) {
26+
if got := checkIsomorphic(tt.args.s, tt.args.t); got != tt.want {
27+
t.Errorf("checkIsomorphic() = %v, want %v", got, tt.want)
28+
}
29+
})
30+
}
31+
}

0 commit comments

Comments
 (0)