s
가 주어졌을 때, s
를 JadenCase로 바꾼 문자열을 리턴하는 함수, solution을 완성해주세요.s
는 길이 1 이상 200 이하인 문자열입니다.s
는 알파벳과 숫자, 공백문자(" ")로 이루어져 있습니다.s | return |
---|---|
"3people unFollowed me" | "3people Unfollowed Me" |
"for the last week" | "For The Last Week" |
function solution(s) {
const strArr = s.split(' ');
return strArr.map(convertToJadenCase).join(' ');
}
function convertToJadenCase(str) {
let JadenCaseStr = '';
for (let i = 0; i < str.length; i++) {
const letter = str[i];
if (i === 0) {
JadenCaseStr += letter.toUpperCase();
} else {
JadenCaseStr += letter.toLowerCase();
}
}
return JadenCaseStr;
}
Given a string s
of '('
, ')'
and lowercase English characters.
Your task is to remove the minimum number of parentheses ( '('
or ')'
, in any positions ) so that the resulting parentheses string is valid and return any valid string.
Formally, a parentheses string is valid if and only if:
It is the empty string, contains only lowercase characters, or
It can be written as AB
(A
concatenated with B
), where A
and B
are valid strings, or
It can be written as (A)
, where A
is a valid string.
Example 1:
Example 2:
Example 3:
1 <= s.length <= 10^5
s[i]
is either '('
, ')'
, or lowercase English letter./**
* @param {string} s
* @return {string}
*/
var minRemoveToMakeValid = function(s) {
const stack = [];
const strArr = s.split('');
for (let i = 0; i < strArr.length; i++) {
if (strArr[i] === '(') {
stack.push(i);
} else if (strArr[i] === ')') {
if (stack.length && strArr[stack[stack.length -1]] === '(') {
stack.pop();
} else {
stack.push(i);
}
}
}
for (const index of stack) {
strArr[index] = '';
}
return strArr.join('');
};
Given a string s
consisting only of characters a
, b
and c
.
Return the number of substrings containing at least one occurrence of all these characters a
, b
and c
.
Example 1:
Example 2:
Example 3:
3 <= s.length <= 5 x 10^4
s
only consists of a
, b
or c
characters./**
* @param {string} s
* @return {number}
*/
var numberOfSubstrings = function(s) {
let aCount = 0, bCount = 0, cCount = 0, result = 0;
for (let left = 0, right = 0; right < s.length; right++) {
if (s[right] === 'a') {
aCount++;
}
if (s[right] === 'b') {
bCount++;
}
if (s[right] === 'c') {
cCount++;
}
while (aCount && bCount && cCount) {
result += s.length - right;
if (s[left] === 'a') {
aCount--;
}
if (s[left] === 'b') {
bCount--;
}
if (s[left] === 'c') {
cCount--;
}
left++;
}
}
return result;
};
s
is nice if, for every letter of the alphabet that s
contains, it appears both in uppercase and lowercase. "abABB"
is nice because 'A'
and 'a'
appear, and 'B'
and 'b'
appear. However, "abA"
is not because 'b'
appears, but 'B'
does not.s
, return the longest substring of s that is nice. If there are multiple, return the substring of the earliest occurrence. If there are none, return an empty string.Example 1:
Example 2:
Example 3:
1 <= s.length <= 100
s
consists of uppercase and lowercase English letters./**
* @param {string} s
* @return {string}
*/
var longestNiceSubstring = function(s) {
const strSet = new Set();
for (const letter of s) {
strSet.add(letter);
}
for (let i = 0; i < s.length; i++) {
const upperCaseLetter = s[i].toUpperCase();
const lowerCaseLetter = s[i].toLowerCase();
if (strSet.has(upperCaseLetter) && strSet.has(lowerCaseLetter)) {
continue;
}
const prevStr = longestNiceSubstring(s.substring(0, i));
const nextStr = longestNiceSubstring(s.substring(i + 1));
return prevStr.length >= nextStr.length ? prevStr : nextStr;
}
return s;
};