Skip to main content

31 posts tagged with "string"

View All Tags

· One min read

https://leetcode.com/problems/valid-parentheses/

Solution 1

/**
* @param {string} s
* @return {boolean}
*/
var isValid = function (s) {
if (s.length % 2 !== 0) {
return false;
}

const stack = [];
const brackets = {
'(': ')',
'{': '}',
'[': ']',
};

for (let i = 0; i < s.length; i++) {
if (Object.keys(brackets).includes(s[i])) {
stack.push(s[i]);
} else {
if (brackets[stack[stack.length - 1]] === s[i]) {
stack.pop();
} else {
return false;
}
}
}
return stack.length === 0;
};

Runtime: 240 ms

Memory Usage: 51.4 MB

· One min read

https://leetcode.com/problems/valid-anagram/

Solution 1

/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
if (s.length !== t.length) {
return false;
}

const hashMapS = {};
const hashMapT = {};

for (let i = 0; i < s.length; i++) {
if (hashMapS[s[i]]) {
hashMapS[s[i]] += 1;
} else {
hashMapS[s[i]] = 1;
}

if (hashMapT[t[i]]) {
hashMapT[t[i]] += 1;
} else {
hashMapT[t[i]] = 1;
}
}

const compareST = Object.entries(hashMapS).map(([key, value]) => {
console.log(value, hashMapT[key]);
console.log(value === hashMapT[key]);
if (value === hashMapT[key]) {
return true;
}
return false;
});

return compareST.every((b) => b);
};

· One min read

https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/

Solution 1

/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function (haystack, needle) {
if (haystack.length === 0) {
if (needle.length === 0) return 0;
return -1;
}
if (haystack.length !== 0) {
if (needle.length === 0) return 0;
}

let index;
const needleFirstStr = needle[0];

for (let i = 0; i < haystack.length; i++) {
if (haystack[i] === needleFirstStr) {
if (haystack.substring(i, i + needle.length) === needle) {
index = i;
break;
} else {
index = -1;
}
} else {
index = -1;
}
}

return index;
};

· One min read

https://leetcode.com/problems/word-pattern/

Solution 1

/**
* @param {string} pattern
* @param {string} s
* @return {boolean}
*/
var wordPattern = function (pattern, s) {
const splitS = s.split(' ');

if (splitS.length !== pattern.length) {
return false;
}

if (new Set(pattern.split('')).size !== new Set(splitS).size) {
return false;
}

const hashMap = {};
for (let i = 0; i < pattern.length; i++) {
if (hashMap[pattern[i]]) {
if (hashMap[pattern[i]] !== splitS[i]) {
return false;
}
} else {
hashMap[pattern[i]] = splitS[i];
}
}

return true;
};

· One min read

https://leetcode.com/problems/first-unique-character-in-a-string/

Solution 1

/**
* @param {string} s
* @return {number}
*/
var firstUniqChar = function (s) {
const hashMap = {};
for (let i = 0; i < s.length; i++) {
if (hashMap[s[i]]) {
hashMap[s[i]] = {
...hashMap[s[i]],
count: hashMap[s[i]].count + 1,
};
} else {
hashMap[s[i]] = {
index: i,
count: 1,
};
}
}

const filteredResult = Object.entries(hashMap)
.map(([key, value]) => {
if (value.count === 1) {
return value.index;
}
})
.filter((value) => value !== false && value !== undefined);

return filteredResult.length === 0 ? -1 : filteredResult[0];
};