s
and t
, return true
if s
is a subsequence of t
, or false
otherwise.A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace"
is a subsequence of "abcde"
while "aec"
is not).
Example 1:
Input: s = "abc", t = "ahbgdc"
Output: true
Example 2:
Input: s = "axc", t = "ahbgdc"
Output: false
0 <= s.length <= 100
0 <= t.length <= 10^4
s
and t
consist only of lowercase English letters./**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isSubsequence = function(s, t) {
let sIndex = 0;
for (let tIndex = 0; tIndex < t.length; tIndex++) {
const str = t[tIndex];
if (s[sIndex] === str) {
sIndex++;
}
}
return sIndex === s.length;
};
Given a string s
and an array of strings words
, return the number of words[i]
that is a subsequence of s
.
A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters.
For example, "ace"
is a subsequence of "abcde"
.
Example 1:
Example 2:
1 <= s.length <= 5 * 10^4
1 <= words.length <= 5000
1 <= words[i].length <= 50
s
and words[i]
consist of only lowercase English letters./**
* @param {string} s
* @param {string[]} words
* @return {number}
*/
var numMatchingSubseq = function(s, words) {
return words.reduce((sum, word) => {
let wordIndex = 0;
for (let sIndex = 0; sIndex < s.length; sIndex++) {
const str = s[sIndex];
if (word[wordIndex] === str) {
wordIndex++;
}
}
if (wordIndex === word.length) {
sum += 1;
}
return sum;
}, 0);
};
var numMatchingSubseq = function(s, words) {
let subsequenceCount = 0;
for (const word of words) {
if (isWordSubsequence(s, word)) {
subsequenceCount++;
}
}
return subsequenceCount;
};
function isWordSubsequence(s, word) {
let index = -1;
for (const str of word) {
const strIndex = s.indexOf(str, index + 1);
if (strIndex === -1) {
return false;
}
index = strIndex;
}
return true;
}
You are given a string s
. We want to partition the string into as many parts as possible so that each letter appears in at most one part.
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s
.
Return a list of integers representing the size of these parts.
Example 1:
Example 2:
1 <= s.length <= 500
s
consists of lowercase English letters.var partitionLabels = function(s) {
const answer = [];
let lastIndex = -1;
let startIndex = 0;
for (let index = 0; index < s.length; index++) {
lastIndex = Math.max(s.lastIndexOf(s[index]), lastIndex);
if (index === lastIndex) {
answer.push(index - startIndex + 1);
startIndex = index + 1;
}
}
return answer;
};
intervals
where intervals[i] = [starti, endi]
, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.Example 1:
Example 2:
1 <= intervals.length <= 10^4
intervals[i].length == 2
0 <= starti <= endi <= 10^4
/**
* @param {number[][]} intervals
* @return {number[][]}
*/
var merge = function(intervals) {
if (intervals.length < 2) {
return intervals;
}
intervals.sort((a, b) => a[0] - b[0]);
for (let i = 1; i < intervals.length; i++) {
const curr = intervals[i];
const prev = intervals[i - 1];
if (curr[0] <= prev[1]) {
intervals[i] = [Math.min(prev[0], curr[0]), Math.max(prev[1], curr[1])];
intervals.splice(i - 1, 1);
i--;
}
}
return intervals;
}