r/csinterviewproblems • u/tyroo • Dec 18 '15
[DP] Find all interpretations of a string
Given a map and a string find all interpretations of the string
{ "1": a,
"2": b,
"3": c,
...,
"26": z }
Example 1:
"112" - 3 interpretations
"al", "aab", "kb"
Example 2:
"345" - 1 interpretation
"cdf"Assume the mapping is already given to you and passed in as a parameter
Can be solved in O(n) runtime
Source: Big 4 interview
4
Upvotes
2
u/rcheu Dec 18 '15 edited Dec 18 '15
The naive solution is to define f(n) = possible interpretations of the string starting at digit n, f(n) = sum(f(n+x) for all positive integer x such that string[n:n+x] is a valid mapping) + 1 if string[n:] is a valid mapping. After memoization, this runs in O(n * k) where k is the length of the longest mapping. My gut instinct is that you cannot remove the k term for arbitrary mappings (for instance what if mapping has a:1, b:11, c:111, d:1111).
2
u/[deleted] Dec 18 '15
This is a pet peeve of mine. This is not Dynamic Programming. This is an example of memoization.
Dynamic Programming, like all the other "programming" algorithms such as integer programming, linear programming, etc., is an optimization algorithm. There needs to be an objective function you are optimizing over, and yes memoization is essential to DP, but it is separate from it.
1
u/parlezmoose Dec 19 '15
Use a recursive function "combo" that accepts the string, and an index start. At each call, create two variables: c1 = map[str[ind]], c2 = map[str[ind] + str[ind + 1]].
Then create a variable subcombos = combos(map, str, ind + 1). These are the combos for the rest of the string.
Finally, return all combinations of [c1, c2] and subcombos
1
u/qibag Dec 21 '15
Let M be the given string of length n. Let L be a list of lists of interpretations. In order to find the final list of interpretations represented as L[n-1], we have to use the results from L[i], i = 0 to n-2. Let L[i] represent the list of interpretations at character i in string M.
The first list {L[i-1] + M[i]} represents the current character added on to the interpretations from L[i-1]. The second list {L[i-2] + P} represents making a character from the previous and current number and adding that to the list of interpretations from L[i-2]. The final list of interpretations will be formed at L[n-1].
Runtime: O( n2 ), because at character i, we will be adding to at least i interpretations. 1+2+...+n = n(n+1)/2
Space: O( n2 ), because at each character i, we can generate at least i interpretations. 1+2+...+n = n(n+1)/2
That's the best I got. I'm not sure how to do it in O(n).
1
u/sir_codes_alot Dec 31 '15 edited Dec 31 '15
Tossing my solution up. Seems to work:
I'll try for a DP solution later.
EDIT Took a good 45 mins to rework it (so that's a fail, but I already know I'm weak on DP), but here is a DP version:
1
u/[deleted] Mar 31 '16
This can be solved by dynamic programming with O(n) time complexity and O(1) space complexity. Define A[i] as the number of ways to interpret string s[i:n-1]. Define two functions: valid1(char c) which returns 1 if c is '1'~'9' and 0 otherwise; valid2(char c1, char c2) which returns 1 if c1,c2 is '10' ~ '26' and 0 otherwise.
The base solution is A[n-1] = valid1(s[n-1]), A[n-2] = valid2(s[n-2], s[n-1]) + valid1(s[n-2])*A[n-1]
Then we iterate from n-2 to 0 with A[i] = valid1(s[i])*A[i+1] + valid2(s[i], s[i+1])*A[i+2] The solution is given at A[0].
It require one sweep over the string, so the time complexity is O(n). The formula requires the results of only previous two iterations, so the space complexity is O(1) if you don't actually allocate A.