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

Conversation

aadil42
Copy link
Contributor

@aadil42 aadil42 commented Jun 21, 2023

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/

Solved decode-string in JS.
*/
var decodeString = function(s) {

const myStack = [];
Copy link
Collaborator

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

for(let i = 0; i < s.length; i++) {
if(s[i] !== ']') {
myStack.push(s[i]);
} else {
Copy link
Collaborator

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;

...

Copy link
Contributor Author

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.

Copy link
Contributor Author

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
*/
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.

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

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.

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?

Using array instead of strings.
while(myStack[myStack.length - 1] !== '[') {
subStr.push(myStack.pop());
}
subStr = subStr.reverse();
Copy link
Collaborator

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)
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants