It validates if a substring in a string is a valid string (this is including all types of brackets but no other symbols)
// Using a regex so we can add any other characters needed also const regex = /^[a-zA-Z {}() \[\] ]+$/; export const validateString = (input: string, index: number): boolean => { const substr = input.substring(index, input.length - index); return regex.test(substr); };
export function validateString(input: string, index: number): boolean {const validator = new BracketsValidator();return [...input].splice(index, input.length-index).every(e=>validator.isValidChar(e));}- // Using a regex so we can add any other characters needed also
- const regex = /^[a-zA-Z {}() \[\] ]+$/;
class BracketsValidator {private readonly bracketPairs: Record<string, string> = {'{': '}','[': ']','(': ')',};private readonly openBrackets: Set<string> = new Set(Object.keys(this.bracketPairs));private readonly closingBrackets: Set<string> = new Set(Object.values(this.bracketPairs));private stack: string[] = [];public isValidChar(char: string): boolean {if (this.isNotBracket(char)) {return true;}if (this.isOpeningBracket(char)) {this.push(char);return true;}if (this.hasMatchFor(char)) {this.pop();return true;}return false;}private push(bracket: string): void {this.stack.push(bracket);}private pop(): string|undefined {return this.stack.pop();}private lastFromStack(): string|undefined {return this.stack.slice(-1)[0];}private hasMatchFor(bracket: string): boolean {const previousBracket: string|undefined = this.lastFromStack();if (undefined === previousBracket) {return false;}const match = this.matchFor(bracket);return previousBracket === match;}private isOpeningBracket(char: string): boolean {return this.openBrackets.has(char);}private isClosingBracket(char: string): boolean {return this.closingBrackets.has(char)}private isNotBracket(char: string): boolean {return !(this.isOpeningBracket(char)||this.isClosingBracket(char));}private matchFor(bracket: string): string {const index: number = Object.values(this.bracketPairs).indexOf(bracket);return Object.keys(this.bracketPairs)[index];}}- export const validateString = (input: string, index: number): boolean => {
- const substr = input.substring(index, input.length - index);
- return regex.test(substr);
- };
// See https://www.chaijs.com for how to use Chai. import { assert } from "chai"; import { validateString } from "./solution"; describe('validateString', function() { it('should validate all the characters in each string as valid strings', function() { assert.strictEqual(validateString('{([])}', 0), true, 'A'); assert.strictEqual(validateString('(A[B{C}D]E)', 0), true, 'B'); assert.strictEqual(validateString('a(b[d{a+b]/2]/4)/1', 0), false, 'C'); assert.strictEqual(validateString('[}{})()', 5), true, 'D'); }); });
- // See https://www.chaijs.com for how to use Chai.
- import { assert } from "chai";
- import { validateString } from "./solution";
- describe('validateString', function() {
it('test', function() {- it('should validate all the characters in each string as valid strings', function() {
- assert.strictEqual(validateString('{([])}', 0), true, 'A');
- assert.strictEqual(validateString('(A[B{C}D]E)', 0), true, 'B');
- assert.strictEqual(validateString('a(b[d{a+b]/2]/4)/1', 0), false, 'C');
- assert.strictEqual(validateString('[}{})()', 5), true, 'D');
- });
- });