Ad
Code
Diff
  • const bracketPairs: Record<string, string> = {
      '{': '}',
      '[': ']',
      '(': ')',
    }
    const openBrackets: Set<string> = new Set(Object.keys(bracketPairs));
    const closingBrackets: Set<string> = new Set(Object.values(bracketPairs));
    
    export function validateString(input: string, index: number): boolean {
      const stack: string[] = [];
      for (let i = index, len = input.length; i < len; i++) {
        const char = input[i];
        if (openBrackets.has(char)) {
          stack.push(bracketPairs[char])
        } else if (closingBrackets.has(char)) {
          const match = stack.pop();
          if (match !== char) {
            return false;
          }
        }
      }
      
      return true;
    }
    • export function validateString(input: string, index: number, stack: number = 0): boolean {
    • // TODO: your code
    • if(index === 0 && (input[index] === ')' | input[index] === '}' | input[index] === ']')) {
    • return false;
    • const bracketPairs: Record<string, string> = {
    • '{': '}',
    • '[': ']',
    • '(': ')',
    • }
    • const openBrackets: Set<string> = new Set(Object.keys(bracketPairs));
    • const closingBrackets: Set<string> = new Set(Object.values(bracketPairs));
    • export function validateString(input: string, index: number): boolean {
    • const stack: string[] = [];
    • for (let i = index, len = input.length; i < len; i++) {
    • const char = input[i];
    • if (openBrackets.has(char)) {
    • stack.push(bracketPairs[char])
    • } else if (closingBrackets.has(char)) {
    • const match = stack.pop();
    • if (match !== char) {
    • return false;
    • }
    • }
    • }
    • if(input[index] === '(' | input[index] === '{' | input[index] === '[') {
    • stack++;
    • }
    • validateString(input, index + 1, )
    • return true;
    • }