Given an array of strings strs
, group the anagrams together. You can return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
Example 2:
Example 3:
1 <= strs.length <= 10^4
0 <= strs[i].length <= 100
strs[i]
consists of lowercase English letters./**
* @param {string[]} strs
* @return {string[][]}
*/
var groupAnagrams = function(strs) {
let anagramObj = {};
for (const word of strs) {
const sortedWord = word.split('').sort().join('');
if (anagramObj[sortedWord]) {
anagramObj[sortedWord].push(word);
} else {
anagramObj[sortedWord] = [word];
}
}
return Object.values(anagramObj);
};
You are given a string s
, which contains stars *
.
In one operation, you can:
Choose a star in s
.
Note:
Example 1:
"leet**cod*e"
"leet**cod*e"
. s becomes "lee*cod*e"
."lee*cod*e"
. s becomes "lecod*e"
."lecod*e"
. s becomes "lecoe"
."lecoe"
.Example 2:
"erase*****"
1 <= s.length <= 10^5
s
consists of lowercase English letters and stars *
.s
./**
* @param {string} s
* @return {string}
*/
var removeStars = function(s) {
const stack = [];
for (let i = 0; i < s.length; i++) {
const str = s[i];
if (str === '*') {
stack.pop();
} else {
stack.push(str)
}
}
return stack.join('');
};
You are given row x col
grid
representing a map where grid[i][j] = 1
represents land and grid[i][j] = 0
represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid
is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
row == grid.length
col == grid[i].length
1 <= row, col <= 100
grid[i][j]
is 0
or 1
.grid
./**
* @param {number[][]} grid
* @return {number}
*/
var islandPerimeter = function(grid) {
const row = grid.length;
const col = grid[0].length;
let totalPerimeter = 0;
const getPerimeter = function(rowIndex, colIndex) {
const dx = [1, -1, 0, 0];
const dy = [0, 0, 1, -1];
let perimeter = 4;
for (let i = 0; i < dx.length; i++) {
const nextRow = rowIndex + dx[i];
const nextCol = colIndex + dy[i];
if (nextRow < 0 || nextRow >= row || nextCol < 0 || nextCol >= col) {
continue;
}
if (grid[nextRow][nextCol]) {
perimeter -= 1;
}
}
return perimeter;
}
for (let i = 0; i < row; i++) {
for (let j = 0; j < col; j++) {
if (!grid[i][j]) {
continue;
}
totalPerimeter += getPerimeter(i, j);
}
}
return totalPerimeter;
};