Skip to content

Commit 0d992b0

Browse files
committed
feat: add No.205
1 parent d950d45 commit 0d992b0

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

201-300/205. Isomorphic Strings.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 205. Isomorphic Strings
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Hash Table, String.
5+
- Similar Questions: Word Pattern.
6+
7+
## Problem
8+
9+
Given two strings `s` and `t`, **determine if they are isomorphic**.
10+
11+
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
12+
13+
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.
14+
15+
 
16+
Example 1:
17+
```
18+
Input: s = "egg", t = "add"
19+
Output: true
20+
```Example 2:
21+
```
22+
Input: s = "foo", t = "bar"
23+
Output: false
24+
```Example 3:
25+
```
26+
Input: s = "paper", t = "title"
27+
Output: true
28+
```
29+
 
30+
**Constraints:**
31+
32+
33+
34+
- `1 <= s.length <= 5 * 104`
35+
36+
- `t.length == s.length`
37+
38+
- `s` and `t` consist of any valid ascii character.
39+
40+
41+
42+
## Solution
43+
44+
```javascript
45+
/**
46+
* @param {string} s
47+
* @param {string} t
48+
* @return {boolean}
49+
*/
50+
var isIsomorphic = function(s, t) {
51+
if (s.length !== t.length) return false;
52+
var map = {};
53+
var used = {};
54+
for (var i = 0; i < s.length; i++) {
55+
if (map[s[i]]) {
56+
if (map[s[i]] !== t[i]) return false;
57+
} else {
58+
if (used[t[i]]) return false;
59+
used[t[i]] = true;
60+
map[s[i]] = t[i];
61+
}
62+
}
63+
return true;
64+
};
65+
```
66+
67+
**Explain:**
68+
69+
nope.
70+
71+
**Complexity:**
72+
73+
* Time complexity : O(n).
74+
* Space complexity : O(n).

0 commit comments

Comments
 (0)