You own a Goal Parser that can interpret a string command
. The command
consists of an alphabet of "G"
, "()"
and/or "(al)"
in some order. The Goal Parser will interpret "G"
as the string "G"
, "()"
as the string "o"
, and "(al)"
as the string "al"
. The interpreted strings are then concatenated in the original order.
Given the string command
, return the Goal Parser's interpretation of command
.
Example 1:
Example 2:
Example 3:
1 <= command.length <= 100
command
consists of "G"
, "()"
, and/or "(al)"
in some order./**
* @param {string} command
* @return {string}
*/
var interpret = function(command) {
const strArr = [];
let str = '';
for (const letter of command) {
if (letter === 'G') {
strArr.push(letter);
str = '';
} else if (letter === '(') {
str = letter;
} else if (letter === ')') {
str += letter;
str = str.length > 2 ? 'al' : 'o';
strArr.push(str);
str = '';
} else {
str += letter;
}
}
return strArr.join('');
};
/**
* @param {string} command
* @return {string}
*/
var interpret = function(command) {
return command.split('()').join('o').split('(al)').join('al');
};
You are given an m x n
binary matrix grid
, where 0
represents a sea cell and 1
represents a land cell.
A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid
.
Return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.
m == grid.length
n == grid[i].length
1 <= m, n <= 500
grid[i][j]
is either 0
or 1
./**
* @param {number[][]} grid
* @return {number}
*/
var numEnclaves = function(grid) {
const M = grid.length;
const N = grid[0].length;
const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]];
const dfs = (x, y) => {
grid[x][y] = 0;
for (const [dx, dy] of directions) {
const nx = x + dx;
const ny = y + dy;
if (nx >= 0 && nx < M && ny >= 0 && ny < N && grid[nx][ny]) {
dfs(nx, ny);
}
}
}
for (let i = 0; i < M; i++) {
if (grid[i][0]) {
dfs(i, 0);
}
if (grid[i][N - 1]) {
dfs(i, N - 1);
}
}
for (let j = 0; j < N; j++) {
if (grid[0][j]) {
dfs(0, j);
}
if (grid[M - 1][j]) {
dfs(M - 1, j);
}
}
let landCount = 0;
for (let i = 0; i < M; i++) {
for (let j = 0; j < N; j++) {
if (grid[i][j]) {
landCount++;
}
}
}
return landCount;
};
A valid parentheses string is either empty ""
, "(" + A + ")"
, or A + B
, where A
and B
are valid parentheses strings, and +
represents string concatenation.
""
, "()"
, "(())()"
, and "(()(()))"
are all valid parentheses strings.A valid parentheses string s is primitive if it is nonempty, and there does not exist a way to split it into s = A + B
, with A
and B
nonempty valid parentheses strings.
Given a valid parentheses string s
, consider its primitive decomposition: s = P1 + P2 + ... + Pk
, where Pi are primitive valid parentheses strings.
Return s
after removing the outermost parentheses of every primitive string in the primitive decomposition of s
.
Example 1:
Example 2:
Example 3:
1 <= s.length <= 10^5
s[i]
is either '('
or ')'
.s
is a valid parentheses string./**
* @param {string} s
* @return {string}
*/
var removeOuterParentheses = function(s) {
const stack = [];
let openParenthesisCount = 0;
let closeParenthesisCount = 0;
let validParenthesis = '';
for (const letter of s) {
if (letter === '(') {
openParenthesisCount++;
}
if (letter === ')') {
closeParenthesisCount++;
}
if (openParenthesisCount === closeParenthesisCount) {
if (validParenthesis.length) {
stack.push(validParenthesis);
}
validParenthesis = '';
openParenthesisCount = 0;
closeParenthesisCount = 0;
} else {
if (openParenthesisCount > 1) {
validParenthesis += letter;
}
}
}
return stack.join('');
};