Coding questions · Meta
Validate abbreviations and brackets
This question evaluates proficiency in string parsing, pattern validation, and algorithmic correctness, covering abbreviation interpretation and bracket balancing while requiring careful handling of edge cases and input constraints.
The coding round included two short implementation problems:
- Abbreviation validation
Given a lowercase word
wordand a stringabbr, determine whetherabbris a valid abbreviation ofword.
- Letters in
abbrmust match the corresponding letters inword. - A positive integer in
abbrmeans skipping that many characters inword. - Numbers cannot have leading zeros.
- The abbreviation is valid only if it consumes the entire word exactly.
Example:
word = "internationalization",abbr = "i12iz4n"->trueword = "apple",abbr = "a2e"->false
- Balanced bracket validation
Given a string
scontaining only the characters(,),[,],{, and}, return whether the brackets are balanced.
- Every opening bracket must be closed by the same type of bracket.
- Brackets must be closed in the correct order.
- An empty string is considered valid.
Example:
s = "()[]{}"->trues = "([)]"->false
Part 1: Abbreviation Validation
Given a lowercase word word and a string abbr, determine whether abbr is a valid abbreviation of word.
Rules:
- Letters in
abbrmust match the corresponding letters inword. - A positive integer in
abbrmeans skipping that many characters inword. - Numbers cannot have leading zeros.
- The abbreviation is valid only if it consumes the entire word exactly.
Constraints
- 0 <= len(word) <= 100000
- 0 <= len(abbr) <= 100000
wordcontains only lowercase English lettersabbrcontains only lowercase English letters and digits- Any numeric part of
abbrmust represent a positive integer with no leading zeros to be valid
Examples
Example 1
Input: ("internationalization", "i12iz4n")
Output: True
Explanation: i matches, 12 skips 12 letters, i and z match, 4 skips 4 letters, and n matches the final character.
Example 2
Input: ("apple", "a2e")
Output: False
Explanation: After matching a and skipping 2 characters, the next character in word is l, not e.
Hints
- Use two pointers: one for
wordand one forabbr. - When you see a digit, parse the full number first, then advance the word pointer by that amount.
Part 2: Balanced Bracket Validation
Given a string s containing only the characters (, ), [, ], {, and }, return whether the brackets are balanced.
A string is balanced if:
- Every opening bracket is closed by the same type of bracket.
- Brackets are closed in the correct order.
- An empty string is considered valid.
Constraints
- 0 <= len(s) <= 100000
scontains only the characters(,),[,],{, and}
Examples
Example 1
Input: "()[]{}"
Output: True
Explanation: Each bracket is correctly opened and closed.
Example 2
Input: "([)]"
Output: False
Explanation: The order is wrong: ( opens first, but ] tries to close [ before ) closes (.
Hints
- Use a stack to keep track of the opening brackets you have not matched yet.
- When you see a closing bracket, it must match the most recent unmatched opening bracket.