LeetCode 1639: Number of Ways to Form a Target String Given a Dictionary Solution
Master LeetCode problem 1639 (Number of Ways to Form a Target String Given a Dictionary), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1639. Number of Ways to Form a Target String Given a Dictionary
Problem Explanation
Explanation:
To solve this problem, we can use dynamic programming. We will iterate over the target string and for each character in the target, we will maintain a count of ways to form that character. We will update this count based on the characters in the words list.
For each character in the target string, we will iterate over the words list and update the count based on the count of ways to form the previous character. This is because each character in the target can be formed based on the characters formed in the previous positions. We will keep track of the count modulo 10^9 + 7 to avoid overflow.
After iterating over all characters in the target string, the final count will represent the total number of ways to form the target string.
Time Complexity:
The time complexity of this approach is O(n * m * l), where n is the length of the target string, m is the length of the words list, and l is the length of each word.
Space Complexity:
The space complexity of this approach is O(n), where n is the length of the target string.
:
Solution Code
class Solution {
public int numWays(String[] words, String target) {
int mod = 1000000007;
int n = target.length();
int m = words[0].length();
long[][] dp = new long[m + 1][n + 1];
dp[0][0] = 1;
for (int i = 1; i <= m; i++) {
for (int j = 0; j <= n; j++) {
dp[i][j] = dp[i - 1][j];
if (j > 0) {
dp[i][j] += dp[i - 1][j - 1];
}
dp[i][j] %= mod;
}
}
long[] count = new long[n + 1];
for (String word : words) {
for (int i = 0; i < m; i++) {
for (int j = n - 1; j >= 0; j--) {
if (target.charAt(j) == word.charAt(i)) {
count[j + 1] += count[j] + dp[i][j];
count[j + 1] %= mod;
}
}
}
}
return (int) count[n];
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1639 (Number of Ways to Form a Target String Given a Dictionary)?
This page provides optimized solutions for LeetCode problem 1639 (Number of Ways to Form a Target String Given a Dictionary) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1639 (Number of Ways to Form a Target String Given a Dictionary)?
The time complexity for LeetCode 1639 (Number of Ways to Form a Target String Given a Dictionary) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1639 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1639 in Java, C++, or Python.