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 codeif(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;
- }
// See https://www.chaijs.com for how to use Chai. import { assert } from "chai"; import { validateString } from "./solution"; describe('validateString', function() { it('test', function() { assert.strictEqual(validateString('{([])}', 0), true); assert.strictEqual(validateString('(A[B{C}D]E)', 0), true); assert.strictEqual(validateString('a(b[d{a+b]/2]/4)/1', 0), false); assert.strictEqual(validateString('[}{})()', 5), true); }); });
- // See https://www.chaijs.com for how to use Chai.
- import { assert } from "chai";
- import { validateString } from "./solution";
- describe('validateString', function() {
- it('test', function() {
- assert.strictEqual(validateString('{([])}', 0), true);
- assert.strictEqual(validateString('(A[B{C}D]E)', 0), true);
- assert.strictEqual(validateString('a(b[d{a+b]/2]/4)/1', 0), false);
assert.strictEqual(validateString('[}{})()', 0), false);- assert.strictEqual(validateString('[}{})()', 5), true);
- });
- });