Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Last active February 18, 2025 17:09
Show Gist options
  • Save tatsuyax25/eb34cdf7cd544b6d06e2dc0dc44d93f7 to your computer and use it in GitHub Desktop.
Save tatsuyax25/eb34cdf7cd544b6d06e2dc0dc44d93f7 to your computer and use it in GitHub Desktop.
You are given a 0-indexed string pattern of length n consisting of the characters 'I' meaning increasing and 'D' meaning decreasing. A 0-indexed string num of length n + 1 is created using the following conditions: num consists of the digits '1' to
/**
* @param {string} pattern
* @return {string}
*/
var smallestNumber = function(pattern) {
const n = pattern.length;
const result = [];
const stack = [];
for (let i = 0; i <= n; i++) {
// Push the current number onto the stack
stack.push(i + 1);
// If we reach the end or find an 'I', process the stack
if (i === n || pattern[i] === 'I') {
// Pop elements from the stack and add to result
while (stack.length) {
result.push(stack.pop());
}
}
}
// Convert the result array to a string and return
return result.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment