RainX_69

Number of Ways to Form a Target String Given a Dictionary | HARD | OA | MUST DO | TRICKY

Apr 16th, 2023 (edited)
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | Source Code | 0 0
  1. https://leetcode.com/problems/number-of-ways-to-form-a-target-string-given-a-dictionary/
  2.  
  3. You are given a list of strings of the same length words and a string target.
  4. Your task is to form target using the given words under the following rules:
  5. target should be formed from left to right.
  6. To form the ith character (0-indexed) of target, you can choose the kth character of the jth string in words if
  7. target[i] = words[j][k].
  8. 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.
  9. Repeat the process until you form the string target.
  10. Notice that you can use multiple characters from the same string in words provided the conditions above are met.
  11.  
  12. Return the number of ways to form target from words. Since the answer may be too large, return it modulo 10^9 + 7.
  13.  
  14. Example 1:
  15. Input: words = ["abba","baab"], target = "bab"
  16. Output: 4
  17. Explanation: There are 4 ways to form target.
  18. "bab" -> index 0 ("baab"), index 1 ("baab"), index 2 ("abba")
  19. "bab" -> index 0 ("baab"), index 1 ("baab"), index 3 ("baab")
  20. "bab" -> index 0 ("baab"), index 2 ("baab"), index 3 ("baab")
  21. "bab" -> index 1 ("abba"), index 2 ("baab"), index 3 ("baab")
  22.  
  23.  
  24. Constraints:
  25. 1 <= words.length <= 1000
  26. 1 <= words[i].length <= 1000
  27. All strings in words have the same length.
  28. 1 <= target.length <= 1000
  29. words[i] and target contain only lowercase English letters.
  30.  
  31.  
  32.  
  33. --------------------------------------------------------------------------------------------------------------------------------------
  34.  
  35.  
  36. class Solution {
  37. public:
  38.     long long dp[1001][1001];
  39.    
  40.     long long helper(vector<string> &words, int wordInd, int currS, string &target){
  41.         if(currS==target.size()){
  42.             return 1;
  43.         }
  44.         if(wordInd==words[0].size()){
  45.             return 0;
  46.         }
  47.         if(dp[wordInd][currS]!=-1){
  48.             return dp[wordInd][currS];
  49.         }
  50.         long long res=helper(words,wordInd+1,currS,target);  // ignoring this index
  51.        
  52.         for(auto word: words){
  53.             if(word[wordInd]==target[currS]){
  54.                 res=(res+helper(words,wordInd+1,currS+1,target))%1000000007;
  55.             }
  56.         }
  57.         return dp[wordInd][currS]=res;
  58.     }
  59.    
  60.     int numWays(vector<string>& words, string target) {
  61.         memset(dp,-1,sizeof(dp));
  62.         return helper(words,0,0,target);
  63.     }
  64. }; // BRUTE FORCE (TLE)
  65.  
  66. ---------------------------------------------------------------------------------------------------------------------------------------
  67.  
  68. class Solution {
  69. public:
  70.     int dp[1001][1001];
  71.  
  72.     long long int helper(vector<vector<int>> &freq, string &target, int curr, int K, int n){
  73.         if(curr==target.size()){
  74.             return 1;
  75.         }
  76.         if(K==freq.size()){
  77.             return 0;
  78.         }
  79.         if(dp[K][curr]!=-1){
  80.             return dp[K][curr];
  81.         }
  82.         long long int ACCEPT=(freq[K][target[curr]-'a']*helper(freq,target,curr+1,K+1,n))%1000000007;
  83.         long long int IGNORE=helper(freq,target,curr,K+1,n)%1000000007;
  84.  
  85.         return dp[K][curr]=(ACCEPT+IGNORE)%1000000007;
  86.     }
  87.    
  88.     int numWays(vector<string>& words, string target) {
  89.         memset(dp,-1,sizeof(dp));
  90.         int n=words[0].size();
  91.         vector<vector<int>> freq(n,vector<int>(26,0));
  92.         for(auto word: words){
  93.             for(int i=0;i<n;i++){
  94.                 freq[i][word[i]-'a']++;
  95.             }
  96.         }
  97.         return helper(freq,target,0,0,n);
  98.     }
  99. };
Advertisement
Add Comment
Please, Sign In to add comment