Solve Two String Problems
Company: Amazon
Role: Data Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This task evaluates string manipulation and encoding for generating unique Morse representations, along with sentence segmentation and combinatorial enumeration for producing all valid word-break sentences, focusing on transformation, deduplication, and decomposition skills.
Part 1: Unique Morse Code Transformations
Constraints
- 0 <= len(words) <= 100
- 1 <= len(words[i]) <= 12 for every word when the list is non-empty
- Each word contains only lowercase English letters
Examples
Input: (["gin", "zen", "gig", "msg"],)
Expected Output: 2
Explanation: gin and zen share one translation, while gig and msg share another.
Input: (["a"],)
Expected Output: 1
Explanation: A single word always contributes exactly one translation.
Input: ([],)
Expected Output: 0
Explanation: With no words, there are no translations.
Input: (["a", "a", "a"],)
Expected Output: 1
Explanation: Repeated identical words do not create new distinct Morse strings.
Hints
- Store the Morse code for letters a to z in an array so you can index it with ord(ch) - ord('a').
- Use a set to keep only unique translated strings.
Part 2: Word Break II
Constraints
- 0 <= len(s) <= 20
- 0 <= len(wordDict) <= 1000
- 1 <= len(wordDict[i]) <= 10 for every dictionary word when the list is non-empty
- s and all words in wordDict contain only lowercase English letters
- A 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 exactly 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: Three different valid segmentations exist.
Input: ("catsandog", ["cats", "dog", "sand", "and", "cat"])
Expected Output: []
Explanation: No full segmentation reaches the end of the string.
Input: ("", ["a", "b"])
Expected Output: []
Explanation: For this problem, empty input returns no sentences.
Input: ("a", ["a", "aa"])
Expected Output: ["a"]
Explanation: The whole string itself is a valid dictionary word.
Hints
- Think of the problem as: from each starting index, what sentences can be formed from the suffix beginning there?
- Use memoization so you do not recompute all sentence combinations for the same index repeatedly.