KatInterview

Compressed Word Check Plus Bracket Matching

StringsAuto-graded · 25 test cases
MediumAuto-gradedStringstwo-pointersstackstring-parsing

Last updated

Tests two linear scans in one function: validating a numeric-skip word abbreviation with two pointers, and checking nested bracket balance with a stack.

Write a single function that answers two independent yes/no questions and returns both answers in a list.

Part A: Is the shortened form legitimate?

You are given a lowercase string word and a string abbr. Decide whether abbr is a valid shortened form of word, using these rules while reading abbr from left to right:

  • A letter in abbr stands for itself. It must equal the next not-yet-covered character of word.
  • A maximal run of consecutive digits in abbr is read as one decimal number k. It stands for k consecutive characters of word, whose contents are ignored.
  • Every such number must be positive and must not start with the digit 0. So "0", "01", and "a05b" are all invalid.
  • The shortened form is valid only if, once all of abbr has been read, it has covered every character of word exactly, with no characters left over and none overshot.

Part B: Are the brackets balanced?

You are given a string s made up only of the characters (, ), [, ], {, }. It is balanced when:

  • Every opening bracket is closed by a bracket of the same kind.
  • Brackets are closed in the correct nesting order (the most recently opened unclosed bracket must be closed first).
  • The empty string is balanced.

Output

Return a list of two booleans: [abbr_ok, brackets_ok], where abbr_ok is the answer to Part A and brackets_ok is the answer to Part B.

Constraints

  • word contains only lowercase English letters and may be empty.
  • abbr contains only lowercase English letters and digits, and may be empty.
  • s contains only the six bracket characters above and may be empty.
  • Numbers in abbr may be large; do not build the expanded string, simply move a pointer by that amount.
  • The two parts are independent of each other.

Examples

Example 1

Input: check_abbreviation_and_brackets('internationalization', 'i12iz4n', '()[]{}')

Output: [True, True]

Example 2

Input: check_abbreviation_and_brackets('apple', 'a2e', '([)]')

Output: [False, False]

Example 3

Input: check_abbreviation_and_brackets('', '', '')

Output: [True, True]

Hints

Hint 1

For the abbreviation, keep one pointer into `word` and one into `abbr`. A letter advances both after a comparison; a number advances only the `word` pointer by its value.

Hint 2

When you meet a digit, check whether it is '0' first (that rejects both a bare zero and leading zeros), then keep reading digits to form the whole number before moving the word pointer. Stop early if the pointer runs past the end of `word`.

Hint 3

For brackets, push openers on a stack; on a closer, the stack must be non-empty and its top must be the matching opener. At the end the stack must be empty. Don't forget the final full-consumption check in the abbreviation part.