Skip to content

Commit

Permalink
isomorphic-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
aolenevme authored Jan 9, 2023
1 parent d83b3f8 commit d8935bd
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 36 deletions.
22 changes: 11 additions & 11 deletions hashmap/jewels-and-stones.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
59 changes: 59 additions & 0 deletions string/isomorphic-strings.js
Original file line number Diff line number Diff line change
@@ -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;
};
50 changes: 25 additions & 25 deletions string/swap-adjacent-in-lr-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit d8935bd

Please sign in to comment.