diff --git a/hashmap/jewels-and-stones.js b/hashmap/jewels-and-stones.js index 956bc44..0ff47ce 100644 --- a/hashmap/jewels-and-stones.js +++ b/hashmap/jewels-and-stones.js @@ -24,19 +24,19 @@ All the characters of jewels are unique. * @param {string} stones * @return {number} */ -var numJewelsInStones = function(jewels, stones) { - const map = {}; +var numJewelsInStones = function (jewels, stones) { + const map = {}; - for (const jewel of jewels.split('')) { - map[jewel] = jewel; - } + for (const jewel of jewels.split("")) { + map[jewel] = jewel; + } - let counter = 0; - for (const stone of stones.split('')) { - if (map[stone]) { - counter++; - } + let counter = 0; + for (const stone of stones.split("")) { + if (map[stone]) { + counter++; } + } - return counter; + return counter; }; diff --git a/string/isomorphic-strings.js b/string/isomorphic-strings.js new file mode 100644 index 0000000..54883df --- /dev/null +++ b/string/isomorphic-strings.js @@ -0,0 +1,59 @@ +/** +205. Isomorphic Strings + +Given two strings s and t, determine if they are isomorphic. + +Two strings s and t are isomorphic if the characters in s can be replaced to get t. + +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. + +Example 1: +Input: s = "egg", t = "add" +Output: true + +Example 2: +Input: s = "foo", t = "bar" +Output: false + +Example 3: +Input: s = "paper", t = "title" +Output: true + +Constraints: +1 <= s.length <= 5 * 104 +t.length == s.length +s and t consist of any valid ascii character. +*/ + +/** + * @param {string} s + * @param {string} t + * @return {boolean} + */ +var isIsomorphic = function (s, t) { + const s2t = {}; + const t2s = {}; + + for (let i = 0; i < s.length; i++) { + const sChar = s[i]; + const tChar = t[i]; + + if ( + (sChar in s2t && !(tChar in t2s)) || + (tChar in t2s && !(sChar in s2t)) + ) { + return false; + } + + if (sChar in s2t && tChar in t2s) { + if (s2t[sChar] !== tChar || sChar !== t2s[tChar]) { + return false; + } + } else { + s2t[sChar] = tChar; + t2s[tChar] = sChar; + } + } + + return true; +}; diff --git a/string/swap-adjacent-in-lr-string.js b/string/swap-adjacent-in-lr-string.js index ee66207..ad3f072 100644 --- a/string/swap-adjacent-in-lr-string.js +++ b/string/swap-adjacent-in-lr-string.js @@ -28,39 +28,39 @@ Both start and end will only consist of characters in 'L', 'R', and 'X'. * @param {string} end * @return {boolean} */ -var canTransform = function(start, end) { - const first = []; - const second = []; +var canTransform = function (start, end) { + const first = []; + const second = []; - for (let i = 0; i < start.length; i++){ - if (start[i] !== 'X') { - first.push([i, start[i]]); - } + for (let i = 0; i < start.length; i++) { + if (start[i] !== "X") { + first.push([i, start[i]]); } + } - for (let j = 0; j < end.length; j++){ - if(end[j] !== 'X') { - second.push([j, end[j]]); - } + for (let j = 0; j < end.length; j++) { + if (end[j] !== "X") { + second.push([j, end[j]]); } + } - if (first.length !== second.length ) { - return false; - } + if (first.length !== second.length) { + return false; + } - for (let i = 0; i < first.length; i++) { - if (first[i][1] !== second[i][1]) { - return false; - } + for (let i = 0; i < first.length; i++) { + if (first[i][1] !== second[i][1]) { + return false; + } - if (first[i][1] == 'L' && first[i][0] < second[i][0]) { - return false; - } + if (first[i][1] == "L" && first[i][0] < second[i][0]) { + return false; + } - if (first[i][1] == 'R' && first[i][0] > second[i][0]) { - return false; - } + if (first[i][1] == "R" && first[i][0] > second[i][0]) { + return false; } + } - return true; + return true; };