Solve two string DP/hash problems
Company: Amazon
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Overview: This question set evaluates string-processing and algorithmic problem-solving skills, including encoding and transformation for unique representations and dynamic programming, memoization, and backtracking for enumerating all valid segmentations.
Read the full Amazon Data Scientist interview experience this question came from
Part 1: Unique Morse Code Transformations
Constraints
- `0 <= len(words) <= 1000`
- `0 <= len(words[i]) <= 20`
- Each character in every word is a lowercase English letter from `a` to `z`.
Examples
Input: (['gin', 'zen', 'gig', 'msg'],)
Expected Output: 2
Explanation: `'gin'` and `'zen'` map to the same Morse string, and `'gig'` and `'msg'` map to another one.
Input: (['a'],)
Expected Output: 1
Explanation: A single word always contributes exactly one translation.
Hints
- Store the 26 Morse encodings in an array so letter `c` can be found by index `ord('c') - ord('a')`.
- A hash set is enough to track unique translated strings.
Part 2: Word Break II (All Segmentations)
Constraints
- `0 <= len(s) <= 20`
- `0 <= len(wordDict) <= 1000`
- Each word in `wordDict` has length at least 1.
- All strings contain only lowercase English letters.
- The same dictionary word may be reused multiple times.
Examples
Input: ('catsanddog', ['cat', 'cats', 'and', 'sand', 'dog'])
Expected Output: ['cat sand dog', 'cats and dog']
Explanation: There are two valid ways to split the string.
Input: ('pineapplepenapple', ['apple', 'pen', 'applepen', 'pine', 'pineapple'])
Expected Output: ['pine apple pen apple', 'pine applepen apple', 'pineapple pen apple']
Explanation: All three segmentations are valid and reuse of dictionary words is allowed.
Hints
- Think in terms of suffixes: what sentences can be formed starting at index `i`?
- Use memoization so each starting index is solved once instead of recomputing the same suffix repeatedly.