Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://leetcode.com/problems/number-of-ways-to-form-a-target-string-given-a-dictionary/
- You are given a list of strings of the same length words and a string target.
- Your task is to form target using the given words under the following rules:
- target should be formed from left to right.
- To form the ith character (0-indexed) of target, you can choose the kth character of the jth string in words if
- target[i] = words[j][k].
- Once you use the kth character of the jth string of words, you can no longer use the yth character of any string in words where y <= k. In other words, all characters to the left of or at index k become unusuable for every string.
- Repeat the process until you form the string target.
- Notice that you can use multiple characters from the same string in words provided the conditions above are met.
- Return the number of ways to form target from words. Since the answer may be too large, return it modulo 10^9 + 7.
- Example 1:
- Input: words = ["abba","baab"], target = "bab"
- Output: 4
- Explanation: There are 4 ways to form target.
- "bab" -> index 0 ("baab"), index 1 ("baab"), index 2 ("abba")
- "bab" -> index 0 ("baab"), index 1 ("baab"), index 3 ("baab")
- "bab" -> index 0 ("baab"), index 2 ("baab"), index 3 ("baab")
- "bab" -> index 1 ("abba"), index 2 ("baab"), index 3 ("baab")
- Constraints:
- 1 <= words.length <= 1000
- 1 <= words[i].length <= 1000
- All strings in words have the same length.
- 1 <= target.length <= 1000
- words[i] and target contain only lowercase English letters.
- --------------------------------------------------------------------------------------------------------------------------------------
- class Solution {
- public:
- long long dp[1001][1001];
- long long helper(vector<string> &words, int wordInd, int currS, string &target){
- if(currS==target.size()){
- return 1;
- }
- if(wordInd==words[0].size()){
- return 0;
- }
- if(dp[wordInd][currS]!=-1){
- return dp[wordInd][currS];
- }
- long long res=helper(words,wordInd+1,currS,target); // ignoring this index
- for(auto word: words){
- if(word[wordInd]==target[currS]){
- res=(res+helper(words,wordInd+1,currS+1,target))%1000000007;
- }
- }
- return dp[wordInd][currS]=res;
- }
- int numWays(vector<string>& words, string target) {
- memset(dp,-1,sizeof(dp));
- return helper(words,0,0,target);
- }
- }; // BRUTE FORCE (TLE)
- ---------------------------------------------------------------------------------------------------------------------------------------
- class Solution {
- public:
- int dp[1001][1001];
- long long int helper(vector<vector<int>> &freq, string &target, int curr, int K, int n){
- if(curr==target.size()){
- return 1;
- }
- if(K==freq.size()){
- return 0;
- }
- if(dp[K][curr]!=-1){
- return dp[K][curr];
- }
- long long int ACCEPT=(freq[K][target[curr]-'a']*helper(freq,target,curr+1,K+1,n))%1000000007;
- long long int IGNORE=helper(freq,target,curr,K+1,n)%1000000007;
- return dp[K][curr]=(ACCEPT+IGNORE)%1000000007;
- }
- int numWays(vector<string>& words, string target) {
- memset(dp,-1,sizeof(dp));
- int n=words[0].size();
- vector<vector<int>> freq(n,vector<int>(26,0));
- for(auto word: words){
- for(int i=0;i<n;i++){
- freq[i][word[i]-'a']++;
- }
- }
- return helper(freq,target,0,0,n);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment