Solve Two String Problems
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates string manipulation, pattern parsing, and algorithmic problem-solving skills, focusing on edit operations, index and boundary handling, and numeric-token parsing for abbreviations.
Part 1: Exactly One Edit Apart
Constraints
- 0 <= len(s), len(t) <= 100000
- Strings may contain any characters.
- An efficient linear-time solution is expected.
Examples
Input: ('ab', 'acb')
Expected Output: True
Explanation: Insert 'c' into 'ab' to get 'acb'.
Input: ('cab', 'ad')
Expected Output: False
Explanation: More than one edit is required.
Input: ('1203', '1213')
Expected Output: True
Explanation: Replace '0' with '1'.
Input: ('abc', 'abc')
Expected Output: False
Explanation: They are zero edits apart, not exactly one.
Input: ('', 'a')
Expected Output: True
Explanation: Insert one character into the empty string.
Hints
- If the string lengths differ by more than 1, they cannot be exactly one edit apart.
- Use two pointers to scan both strings. At the first mismatch, decide whether it represents a replacement or skipping one character in the longer string.
Part 2: Validate a Word Abbreviation
Constraints
- 0 <= len(word), len(abbr) <= 100000
- `abbr` contains only letters and digits.
- Numbers in `abbr` represent positive skip lengths and cannot contain leading zeros.
Examples
Input: ('internationalization', 'i12iz4n')
Expected Output: True
Explanation: The abbreviation matches the word by skipping the correct number of characters.
Input: ('apple', 'a2e')
Expected Output: False
Explanation: After matching 'a' and skipping 2 characters, the next letter should be 'l', not 'e'.
Input: ('substitution', 's10n')
Expected Output: True
Explanation: Match 's', skip 10 characters, then match 'n'.
Input: ('word', 'w02d')
Expected Output: False
Explanation: The number '02' has a leading zero, which is not allowed.
Input: ('', '')
Expected Output: True
Explanation: An empty abbreviation is valid for an empty word.
Hints
- Walk through both strings with two pointers. When you see digits in `abbr`, parse the whole number before moving on.
- A digit '0' cannot start a number in the abbreviation.