Skip to content

Commit d8935bd

Browse files
authored
isomorphic-strings
1 parent d83b3f8 commit d8935bd

File tree

3 files changed

+95
-36
lines changed

3 files changed

+95
-36
lines changed

hashmap/jewels-and-stones.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ All the characters of jewels are unique.
2424
* @param {string} stones
2525
* @return {number}
2626
*/
27-
var numJewelsInStones = function(jewels, stones) {
28-
const map = {};
27+
var numJewelsInStones = function (jewels, stones) {
28+
const map = {};
2929

30-
for (const jewel of jewels.split('')) {
31-
map[jewel] = jewel;
32-
}
30+
for (const jewel of jewels.split("")) {
31+
map[jewel] = jewel;
32+
}
3333

34-
let counter = 0;
35-
for (const stone of stones.split('')) {
36-
if (map[stone]) {
37-
counter++;
38-
}
34+
let counter = 0;
35+
for (const stone of stones.split("")) {
36+
if (map[stone]) {
37+
counter++;
3938
}
39+
}
4040

41-
return counter;
41+
return counter;
4242
};

string/isomorphic-strings.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
205. Isomorphic Strings
3+
4+
Given two strings s and t, determine if they are isomorphic.
5+
6+
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
7+
8+
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.
9+
10+
Example 1:
11+
Input: s = "egg", t = "add"
12+
Output: true
13+
14+
Example 2:
15+
Input: s = "foo", t = "bar"
16+
Output: false
17+
18+
Example 3:
19+
Input: s = "paper", t = "title"
20+
Output: true
21+
22+
Constraints:
23+
1 <= s.length <= 5 * 104
24+
t.length == s.length
25+
s and t consist of any valid ascii character.
26+
*/
27+
28+
/**
29+
* @param {string} s
30+
* @param {string} t
31+
* @return {boolean}
32+
*/
33+
var isIsomorphic = function (s, t) {
34+
const s2t = {};
35+
const t2s = {};
36+
37+
for (let i = 0; i < s.length; i++) {
38+
const sChar = s[i];
39+
const tChar = t[i];
40+
41+
if (
42+
(sChar in s2t && !(tChar in t2s)) ||
43+
(tChar in t2s && !(sChar in s2t))
44+
) {
45+
return false;
46+
}
47+
48+
if (sChar in s2t && tChar in t2s) {
49+
if (s2t[sChar] !== tChar || sChar !== t2s[tChar]) {
50+
return false;
51+
}
52+
} else {
53+
s2t[sChar] = tChar;
54+
t2s[tChar] = sChar;
55+
}
56+
}
57+
58+
return true;
59+
};

string/swap-adjacent-in-lr-string.js

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,39 @@ Both start and end will only consist of characters in 'L', 'R', and 'X'.
2828
* @param {string} end
2929
* @return {boolean}
3030
*/
31-
var canTransform = function(start, end) {
32-
const first = [];
33-
const second = [];
31+
var canTransform = function (start, end) {
32+
const first = [];
33+
const second = [];
3434

35-
for (let i = 0; i < start.length; i++){
36-
if (start[i] !== 'X') {
37-
first.push([i, start[i]]);
38-
}
35+
for (let i = 0; i < start.length; i++) {
36+
if (start[i] !== "X") {
37+
first.push([i, start[i]]);
3938
}
39+
}
4040

41-
for (let j = 0; j < end.length; j++){
42-
if(end[j] !== 'X') {
43-
second.push([j, end[j]]);
44-
}
41+
for (let j = 0; j < end.length; j++) {
42+
if (end[j] !== "X") {
43+
second.push([j, end[j]]);
4544
}
45+
}
4646

47-
if (first.length !== second.length ) {
48-
return false;
49-
}
47+
if (first.length !== second.length) {
48+
return false;
49+
}
5050

51-
for (let i = 0; i < first.length; i++) {
52-
if (first[i][1] !== second[i][1]) {
53-
return false;
54-
}
51+
for (let i = 0; i < first.length; i++) {
52+
if (first[i][1] !== second[i][1]) {
53+
return false;
54+
}
5555

56-
if (first[i][1] == 'L' && first[i][0] < second[i][0]) {
57-
return false;
58-
}
56+
if (first[i][1] == "L" && first[i][0] < second[i][0]) {
57+
return false;
58+
}
5959

60-
if (first[i][1] == 'R' && first[i][0] > second[i][0]) {
61-
return false;
62-
}
60+
if (first[i][1] == "R" && first[i][0] > second[i][0]) {
61+
return false;
6362
}
63+
}
6464

65-
return true;
65+
return true;
6666
};

0 commit comments

Comments
 (0)