-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Create 0394-decode-string.js #2615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Solved decode-string in JS.
javascript/0394-decode-string.js
Outdated
*/ | ||
var decodeString = function(s) { | ||
|
||
const myStack = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Fix your empty space and stick with consistent tabbing
javascript/0394-decode-string.js
Outdated
for(let i = 0; i < s.length; i++) { | ||
if(s[i] !== ']') { | ||
myStack.push(s[i]); | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: avoid else
if (true) continue;
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting rid of the else case. You truly have some animosity with the else 😅 😅 😅. I have removed it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a kind reminder that I updated the code as suggested.
Getting rid of the else
javascript/0394-decode-string.js
Outdated
*/ | ||
var decodeString = function(s) { | ||
const myStack = []; | ||
let result = ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove unused variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed result var.
javascript/0394-decode-string.js
Outdated
} | ||
myStack.pop(); | ||
|
||
let k = ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid string concatination, use char array instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using array now.
javascript/0394-decode-string.js
Outdated
continue; | ||
} | ||
|
||
let subStr = ''; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid String, use char array instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using array now.
javascript/0394-decode-string.js
Outdated
k = myStack.pop() + k; | ||
} | ||
|
||
myStack.push(subStr.repeat(+k)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Repeat will increase time and space
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so the new time complexity would be n * (the time it takes to get the number of substrings). The space will be the same right?
Using array instead of strings.
while(myStack[myStack.length - 1] !== '[') { | ||
subStr.push(myStack.pop()); | ||
} | ||
subStr = subStr.reverse(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will increase time complexity
* Stack | ||
* https://leetcode.com/problems/decode-string/ | ||
* | ||
* Time O(n) | Space O(n) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review these complexities
/**
* Stack
* Time O(N * (max(K)^count(K))) | Space O(N * sum((max(K)^count(K))))
* @param {string} s
* @return {string}
*/
var decodeString = (s, stack = []) => {
for (const char of s) {
const isClosed = (char === ']');
if (!isClosed) { stack.push(char); continue; }
const string = getString(stack);
const count = getCount(stack);
repeatWord(string, count, stack);
}
return stack.join('');
}
const getString = (stack, word = []) => {
const isOpen = () => (stack[(stack.length - 1)] === '[')
while (!isOpen()) {
word.push(stack.pop());
}
stack.pop();/* ] -> */
return word.reverse();
}
var getCount = (stack, base = 1, count = 0) => {
while (0 < stack.length) {
if (isNaN(Number(stack[(stack.length - 1)]))) break;
count += (getCode(stack.pop()) * base);
base *= 10;
}
return count;
}
var getCode = (char, zero = 48) => (char.charCodeAt(0) - zero);
const repeatWord = (string, count, stack) => {
while ((0 < count)) {
stack.push( ...string );
count -= 1;
}
}
/**
* Stack - Node
* Time O(N * max(K)) | Space O(N + M)
* @param {string} s
* @return {string}
*/
var decodeString = (s, count = 0, stack = [], buffer = []) => {
for (const char of s) {/* Time O(N) */
if (!isNaN(Number(char))) {
count *= 10;
count += getCode(char);
continue;
}
const isOpen = (char === '[');
if (isOpen) {
stack.push([ buffer, count ]);
count = 0;
buffer = [];
continue;
}
const isClosed = (char === ']');
if (isClosed) {
buffer = updateBuffer(stack, buffer);
continue;
}
buffer.push(char);
}
return buffer.join('');
}
var getCode = (char, zero = 48) => (char.charCodeAt(0) - zero);
const updateBuffer = (stack, buffer) => {
const [ string, count ] = stack.pop();
const repeat = buffer.join('')
.repeat(count);
return [ ...string, repeat ];
}
/**
* DFS
* Time O(N * max(K)) | Space O(N)
* @param {string} s
* @return {string}
*/
var decodeString = (s, index = [ 0 ], buffer = []) => {
while (index[0] < s.length) {
const char = s[index[0]];
const isBaseCase = (char === ']');
if (isBaseCase) break;
if (isAlpha(char, index, buffer)) continue;
const string = dfsIndOrder(s, index);
buffer.push(string);
}
return buffer.join('');
}
const isAlpha = (char, index, buffer) => {
if (!isNaN(Number(char))) return false;
buffer.push(char);
index[0] += 1;
return true;
}
const dfsIndOrder = (s, index) => {
const count = getCount(s, index);
index[0] += 1; /* [ -> */
const string = decodeString(s, index);
index[0] += 1; /* ] -> */
return string.repeat(count);
}
var getCode = (char, zero = 48) => (char.charCodeAt(0) - zero);
var getCount = (s, index, count = 0) => {
while (index[0] < s.length) {
const char = s[index[0]];
if (isNaN(Number(char))) break;
count *= 10;
count += getCode(char);
index[0] += 1;
}
return count;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is K?
Solved decode-string in JS.
File(s) Added: 0394-decode-string.js
Language(s) Used: JavaScript
Submission URL: https://leetcode.com/problems/decode-string/submissions/976411909/