Given a string s
, partition the string into one or more substrings such that the characters in each substring are unique. That is, no letter appears in a single substring more than once.
Return the minimum number of substrings in such a partition.
Note that each character should belong to exactly one substring in a partition.
Example 1:
Example 2:
1 <= s.length <= 10^5
s
consists of only English lowercase letters./**
* @param {string} s
* @return {number}
*/
var partitionString = function(s) {
const arr = [];
let substring = '';
for (let i = 0; i < s.length; i++) {
const str = s[i];
if (substring.includes(str)) {
arr.push(substring);
substring = str;
} else {
substring += str;
}
}
if (substring.length) {
arr.push(substring);
}
return arr.length;
};
Alice and Bob play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of stones piles[i]
.
The objective of the game is to end with the most stones. The total number of stones across all the piles is odd, so there are no ties.
Alice and Bob take turns, with Alice starting first. Each turn, a player takes the entire pile of stones either from the beginning or from the end of the row. This continues until there are no more piles left, at which point the person with the most stones wins.
Assuming Alice and Bob play optimally, return true
if Alice wins the game, or false
if Bob wins.
Example 1:
Example 2:
2 <= piles.length <= 500
piles.length
is even.1 <= piles[i] <= 500
sum(piles[i])
is odd./**
* @param {number[]} piles
* @return {boolean}
*/
var stoneGame = function(piles) {
piles.sort((a, b) => b - a);
let Alice = 0
let Bob = 0;
while (piles.length) {
Alice += piles.shift();
Bob += piles.shift();
}
return Alice > Bob;
};
AB
(A
concatenated with B
), where A
and B
are valid strings, or(A)
, where A
is a valid string.s
. In one move, you can insert a parenthesis at any position of the string.s = "()))"
, you can insert an opening parenthesis to be "(()))"
or a closing parenthesis to be "())))"
.s
valid.1 <= s.length <= 1000
s[i]
is either '('
or ')'
./**
* @param {string} s
* @return {number}
*/
var minAddToMakeValid = function(s) {
const stack = [];
for (let i = 0; i < s.length; i++) {
const str = s[i];
if (str === ')' && stack[stack.length - 1] === '(') {
stack.pop();
} else {
stack.push(str);
}
}
return stack.length;
};