Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a6ceb45

Browse files
feat(LC): add 205, notes, optimization, complexity
1 parent fe3ef09 commit a6ceb45

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* @see {@link https://leetcode.com/problems/isomorphic-strings/?envType=study-plan&id=level-1 205. Isomorphic Strings}
3+
*/
4+
5+
/**
6+
* EXPLAIN
7+
* Isomorphic means ONLY key to one value
8+
* In this case, one letter can be mapped to only one letter (self-map ok)
9+
*
10+
* s = "egg", t = "add"
11+
* > IS isomorphic: e -> a, g -> d
12+
*
13+
* s = "foo", t = "bar"
14+
* > IS NOT isomorphic, two unique letters in s, three in t, not uniquely paired
15+
*
16+
* EXTRA EXAMPLE:
17+
*
18+
* s = "baby", t = "fool"
19+
* > IS NOT isomorphic, three unique letters in both, BUT order NOT preserved
20+
*/
21+
22+
/**
23+
* APPROACH
24+
*
25+
* create blank map
26+
*
27+
* both same string length ? return false
28+
*
29+
* LOOP through both words same time
30+
* check to see if map has string 1 letter mapped
31+
* NO: set string 1 letter to string 2 letter
32+
* YES: does string 1 letter key have string 2 letter value
33+
* NO: return false
34+
* END LOOP
35+
*
36+
* made it out of loop; return true
37+
*/
38+
39+
/**
40+
* ORIGINAL CODE
41+
* SPEED: 67ms - 97%
42+
* MEMORY: 46.2MB - 22%
43+
*/
44+
/**
45+
* @param {string} s
46+
* @param {string} t
47+
* @return {boolean}
48+
*/
49+
var isIsomorphic = function (s, t) {
50+
// Both strings need the same number of unique letters for this to work.
51+
const firstLetters = new Set(s);
52+
const secondLetters = new Set(t);
53+
if (firstLetters.size !== secondLetters.size) {
54+
return false;
55+
}
56+
57+
const letterMap = new Map();
58+
59+
for (let i = 0; i < s.length; i++) {
60+
const key = s[i];
61+
const val = t[i];
62+
63+
if (!letterMap.has(key)) {
64+
letterMap.set(key, val);
65+
} else if (letterMap.get(key) !== val) {
66+
return false;
67+
}
68+
}
69+
return true;
70+
};
71+
72+
/**
73+
* Same code, but using an object instead of a Map
74+
* SPEED: 78ms - 80%
75+
* MEMORY: 46MB - 23%
76+
*/
77+
/**
78+
* @param {string} s
79+
* @param {string} t
80+
* @return {boolean}
81+
*/
82+
var isIsomorphic = function (s, t) {
83+
// Both strings need the same number of unique letters for this to work.
84+
const firstUniques = new Set(s);
85+
const secondUniques = new Set(t);
86+
if (firstUniques.size !== secondUniques.size) {
87+
return false;
88+
}
89+
90+
// Can be a regular object b/c insertion order doesn't matter.
91+
const letterMap = {};
92+
93+
// Loop through both strings at same time
94+
for (let i = 0; i < s.length; i++) {
95+
const key = s[i];
96+
const val = t[i];
97+
98+
// IF key does not exist
99+
// set key to val
100+
// ELSE IF existing val for key does not equal current val
101+
// return false
102+
if (!letterMap.hasOwnProperty(key)) {
103+
letterMap[key] = val;
104+
} else if (letterMap[key] !== val) {
105+
return false;
106+
}
107+
}
108+
// made it out of the loop without triggering a false
109+
// strings ARE isomorphic
110+
return true;
111+
};
112+
113+
/**
114+
* COMPLEXITY
115+
* TIME: O(N); to loop through the strings once for Set creation, then key checking
116+
* SPACE: O(N): size of Sets and key-value pairs of map object is dependent on string length
117+
*/
118+
119+
/**
120+
* READ 01:24
121+
* EXPLAIN 02:29 03:53
122+
* APPROACH 08:09 12:03
123+
* CODE 04:39 16:52
124+
* TEST 11:08 28:00
125+
* OPTIMIZE 08:51 36:51
126+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /