-
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?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Stack | ||
* https://leetcode.com/problems/decode-string/ | ||
* | ||
* Time O(n) | Space O(n) | ||
* @param {string} s | ||
* @return {string} | ||
*/ | ||
var decodeString = function(s) { | ||
const myStack = []; | ||
let result = ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. removed result var. |
||
|
||
for(let i = 0; i < s.length; i++) { | ||
if(s[i] !== ']') { | ||
myStack.push(s[i]); | ||
continue; | ||
} | ||
|
||
let subStr = ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. using array now. |
||
while(myStack[myStack.length - 1] !== '[') { | ||
subStr = myStack.pop() + subStr; | ||
} | ||
myStack.pop(); | ||
|
||
let k = ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. using array now. |
||
while(!isNaN(myStack[myStack.length - 1])) { | ||
k = myStack.pop() + k; | ||
} | ||
|
||
myStack.push(subStr.repeat(+k)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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? |
||
} | ||
|
||
return myStack.join(''); | ||
}; |
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
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?