Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions javascript/0394-decode-string.js
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)
Copy link
Collaborator

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;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is K?

* @param {string} s
* @return {string}
*/
var decodeString = function(s) {
const myStack = [];
let result = '';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unused variable

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 = '';
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 = '';
Copy link
Collaborator

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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));
Copy link
Collaborator

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

Copy link
Contributor Author

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?

}

return myStack.join('');
};