From 4224cba61fe18a13cb0e20b0a7c0e52967417d7e Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sun, 4 Aug 2024 15:25:10 +0800 Subject: [PATCH] 187th Commit --- README.md | 3 +- .../394. Decode String/decodeString.test.ts | 9 +++++ src/page-4/394. Decode String/decodeString.ts | 36 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/page-4/394. Decode String/decodeString.test.ts create mode 100644 src/page-4/394. Decode String/decodeString.ts diff --git a/README.md b/README.md index fa54960..93c894a 100644 --- a/README.md +++ b/README.md @@ -143,10 +143,11 @@ Ace Coding Interview with 75 Qs | ---------------------------------- | ---------------- | ------ | | 2390. Removing Stars From a String | [Solution][2390] | Medium | | 735. Asteroid Collision | [Solution][735] | Medium | -| 394. Decode String | Solution | Medium | +| 394. Decode String | [Solution][394] | Medium | [2390]: ./src/page-22/2390.%20Removing%20Stars%20From%20a%20String/removeStars.ts [735]: ./src/page-7/735.%20Asteroid%20Collision/asteroidCollision.ts +[394]: ./src/page-4/394.%20Decode%20String/decodeString.ts | Queue | | | | --------------------------- | -------- | ------ | diff --git a/src/page-4/394. Decode String/decodeString.test.ts b/src/page-4/394. Decode String/decodeString.test.ts new file mode 100644 index 0000000..6d2c95b --- /dev/null +++ b/src/page-4/394. Decode String/decodeString.test.ts @@ -0,0 +1,9 @@ +import { decodeString } from './decodeString'; + +describe('394. Decode String', () => { + test('decodeString', () => { + expect(decodeString('3[a]2[bc]')).toBe('aaabcbc'); + expect(decodeString('3[a2[c]]')).toBe('accaccacc'); + expect(decodeString('2[abc]3[cd]ef')).toBe('abcabccdcdcdef'); + }); +}); diff --git a/src/page-4/394. Decode String/decodeString.ts b/src/page-4/394. Decode String/decodeString.ts new file mode 100644 index 0000000..2906b2b --- /dev/null +++ b/src/page-4/394. Decode String/decodeString.ts @@ -0,0 +1,36 @@ +type DecodeString = (s: string) => string; + +/** + * Accepted + */ +export const decodeString: DecodeString = (s) => { + const countStack: number[] = []; // Stack to store repeat counts + const stringStack: string[] = []; // Stack to store intermediate strings + + let currentNum = 0; // Current number being processed + let currentString = ''; // Current string being processed + + for (const char of s) { + if (!Number.isNaN(Number(char))) { + // If the character is a digit, update currentNum + currentNum = currentNum * 10 + Number(char); + } else if (char === '[') { + // If the character is '[', push currentNum and currentString to stacks + countStack.push(currentNum); + stringStack.push(currentString); + currentNum = 0; // Reset currentNum + currentString = ''; // Reset currentString + } else if (char === ']') { + // If the character is ']', pop from stacks and update currentString + const repeatTimes = countStack.pop(); // Get the last count + const lastString = stringStack.pop(); // Get the last string + currentString = lastString + currentString.repeat(repeatTimes || 1); // Repeat currentString and append + } else { + // If the character is a letter, append it to currentString + currentString += char; + } + } + + // Return the fully decoded string + return currentString; +};