Skip to content

Commit 819690e

Browse files
committed
feat: add solutions to lc problem: No.0205
No.0205.Isomorphic Strings
1 parent e010649 commit 819690e

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

solution/0200-0299/0205.Isomorphic Strings/README.md

+80
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,86 @@ func isIsomorphic(s string, t string) bool {
156156
}
157157
```
158158

159+
### **C#**
160+
161+
```cs
162+
using System.Collections.Generic;
163+
164+
public class Solution {
165+
public bool IsIsomorphic(string s, string t) {
166+
if (s.Length != t.Length) return false;
167+
var dict1 = new Dictionary<char, char>();
168+
var dict2 = new Dictionary<char, char>();
169+
for (var i = 0; i < s.Length; ++i)
170+
{
171+
char mapping1;
172+
char mapping2;
173+
var found1 = dict1.TryGetValue(s[i], out mapping1);
174+
var found2 = dict2.TryGetValue(t[i], out mapping2);
175+
if (found1 ^ found2) return false;
176+
if (!found1)
177+
{
178+
dict1.Add(s[i], t[i]);
179+
dict2.Add(t[i], s[i]);
180+
}
181+
else if (mapping1 != t[i] || mapping2 != s[i])
182+
{
183+
return false;
184+
}
185+
}
186+
return true;
187+
}
188+
}
189+
```
190+
191+
### **TypeScript**
192+
193+
```ts
194+
function isIsomorphic(s: string, t: string): boolean {
195+
const n = s.length;
196+
const help = (s: string, t: string) => {
197+
const map = new Map();
198+
for (let i = 0; i < n; i++) {
199+
if (map.has(s[i])) {
200+
if (map.get(s[i]) !== t[i]) {
201+
return false;
202+
}
203+
} else {
204+
map.set(s[i], t[i]);
205+
}
206+
}
207+
return true;
208+
};
209+
return help(s, t) && help(t, s);
210+
}
211+
```
212+
213+
### **Rust**
214+
215+
```rust
216+
use std::collections::HashMap;
217+
impl Solution {
218+
fn help(s: &[u8], t: &[u8]) -> bool {
219+
let mut map = HashMap::new();
220+
for i in 0..s.len() {
221+
if map.contains_key(&s[i]) {
222+
if map.get(&s[i]).unwrap() != &t[i] {
223+
return false;
224+
}
225+
} else {
226+
map.insert(s[i], t[i]);
227+
}
228+
}
229+
true
230+
}
231+
232+
pub fn is_isomorphic(s: String, t: String) -> bool {
233+
let (s, t) = (s.as_bytes(), t.as_bytes());
234+
Self::help(s, t) && Self::help(t, s)
235+
}
236+
}
237+
```
238+
159239
### **...**
160240

161241
```

solution/0200-0299/0205.Isomorphic Strings/README_EN.md

+80
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,86 @@ func isIsomorphic(s string, t string) bool {
135135
}
136136
```
137137

138+
### **C#**
139+
140+
```cs
141+
using System.Collections.Generic;
142+
143+
public class Solution {
144+
public bool IsIsomorphic(string s, string t) {
145+
if (s.Length != t.Length) return false;
146+
var dict1 = new Dictionary<char, char>();
147+
var dict2 = new Dictionary<char, char>();
148+
for (var i = 0; i < s.Length; ++i)
149+
{
150+
char mapping1;
151+
char mapping2;
152+
var found1 = dict1.TryGetValue(s[i], out mapping1);
153+
var found2 = dict2.TryGetValue(t[i], out mapping2);
154+
if (found1 ^ found2) return false;
155+
if (!found1)
156+
{
157+
dict1.Add(s[i], t[i]);
158+
dict2.Add(t[i], s[i]);
159+
}
160+
else if (mapping1 != t[i] || mapping2 != s[i])
161+
{
162+
return false;
163+
}
164+
}
165+
return true;
166+
}
167+
}
168+
```
169+
170+
### **TypeScript**
171+
172+
```ts
173+
function isIsomorphic(s: string, t: string): boolean {
174+
const n = s.length;
175+
const help = (s: string, t: string) => {
176+
const map = new Map();
177+
for (let i = 0; i < n; i++) {
178+
if (map.has(s[i])) {
179+
if (map.get(s[i]) !== t[i]) {
180+
return false;
181+
}
182+
} else {
183+
map.set(s[i], t[i]);
184+
}
185+
}
186+
return true;
187+
};
188+
return help(s, t) && help(t, s);
189+
}
190+
```
191+
192+
### **Rust**
193+
194+
```rust
195+
use std::collections::HashMap;
196+
impl Solution {
197+
fn help(s: &[u8], t: &[u8]) -> bool {
198+
let mut map = HashMap::new();
199+
for i in 0..s.len() {
200+
if map.contains_key(&s[i]) {
201+
if map.get(&s[i]).unwrap() != &t[i] {
202+
return false;
203+
}
204+
} else {
205+
map.insert(s[i], t[i]);
206+
}
207+
}
208+
true
209+
}
210+
211+
pub fn is_isomorphic(s: String, t: String) -> bool {
212+
let (s, t) = (s.as_bytes(), t.as_bytes());
213+
Self::help(s, t) && Self::help(t, s)
214+
}
215+
}
216+
```
217+
138218
### **...**
139219

140220
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use std::collections::HashMap;
2+
impl Solution {
3+
fn help(s: &[u8], t: &[u8]) -> bool {
4+
let mut map = HashMap::new();
5+
for i in 0..s.len() {
6+
if map.contains_key(&s[i]) {
7+
if map.get(&s[i]).unwrap() != &t[i] {
8+
return false;
9+
}
10+
} else {
11+
map.insert(s[i], t[i]);
12+
}
13+
}
14+
true
15+
}
16+
17+
pub fn is_isomorphic(s: String, t: String) -> bool {
18+
let (s, t) = (s.as_bytes(), t.as_bytes());
19+
Self::help(s, t) && Self::help(t, s)
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function isIsomorphic(s: string, t: string): boolean {
2+
const n = s.length;
3+
const help = (s: string, t: string) => {
4+
const map = new Map();
5+
for (let i = 0; i < n; i++) {
6+
if (map.has(s[i])) {
7+
if (map.get(s[i]) !== t[i]) {
8+
return false;
9+
}
10+
} else {
11+
map.set(s[i], t[i]);
12+
}
13+
}
14+
return true;
15+
};
16+
return help(s, t) && help(t, s);
17+
}

0 commit comments

Comments
 (0)