-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kenny Torng
committed
Oct 11, 2020
1 parent
f4f5af6
commit 2183553
Showing
46 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. | ||
Note: This question is the same as 1081: https://leetcode.com/problems/smallest-subsequence-of-distinct-characters/ | ||
Example 1: | ||
Input: s = "bcabc" | ||
Output: "abc" | ||
Example 2: | ||
Input: s = "cbacdcbc" | ||
Output: "acdb" | ||
Constraints: | ||
1 <= s.length <= 104 | ||
s consists of lowercase English letters. | ||
*/ | ||
/** | ||
* @param {string} s | ||
* @return {string} | ||
*/ | ||
var removeDuplicateLetters = function(s) { | ||
const counts = {}; | ||
const stack = []; | ||
const seen = new Set(); | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
counts[s[i]] = counts[s[i]] + 1 || 1; | ||
} | ||
// b c a b c | ||
// scan letters | ||
// for each letter existing in the stack, if there are remaining counts for it, and it is greater than current, pop | ||
// keep track of seen to prevent adding later dupes | ||
for (let i = 0; i < s.length; i++) { | ||
counts[s[i]]--; | ||
// do not add this dupe | ||
if (seen.has(s[i])) { | ||
continue; | ||
} | ||
while (stack.length && stack[stack.length-1] > s[i] && counts[stack[stack.length-1]] > 0) { | ||
const c = stack.pop(); | ||
// unmark | ||
seen.delete(c); | ||
} | ||
stack.push(s[i]); | ||
seen.add(s[i]); | ||
} | ||
|
||
return stack.join(''); | ||
}; | ||
|
||
/* | ||
b c a b c a | ||
{ | ||
a: 0 | ||
b: 0 | ||
c: 0 | ||
} | ||
{ a b } | ||
[ a c ] | ||
*/ |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* @param {character[][]} board | ||
* @param {string} word | ||
* @return {boolean} | ||
*/ | ||
var exist = function(board, word) { | ||
const rows = board.length; | ||
const cols = board[0].length; | ||
|
||
let cur = 0; | ||
for (let i = 0; i < rows; i++) { | ||
for (let j = 0; j < cols; j++) { | ||
if (board[i][j] === word[0]) { | ||
if (search(i, j, 0)) { | ||
return true; | ||
} | ||
} | ||
|
||
} | ||
} | ||
|
||
return false; | ||
|
||
function search(i, j, c, visited = new Set()) { | ||
if (i < 0 || i === rows || j < 0 || j === cols || visited.has(`${i}-${j}`)) { | ||
return false; | ||
} | ||
// console.log(i, j, board[i][j], word[c], visited) | ||
if (c === word.length - 1 && board[i][j] === word[c]) { | ||
return true; | ||
} | ||
let found = true; | ||
|
||
visited.add(`${i}-${j}`); | ||
found = found && board[i][j] === word[c] && ( | ||
search(i-1, j, c+1, visited) || | ||
search(i, j-1, c+1, visited) || | ||
search(i+1, j, c+1, visited) || | ||
search(i, j+1, c+1, visited) | ||
); | ||
visited.delete(`${i}-${j}`); | ||
|
||
return found; | ||
} | ||
}; | ||
|
||
console.log( | ||
exist( | ||
[["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], | ||
"ABCCED" | ||
) | ||
); |