Advertisement
rishu110067

Untitled

Jan 21st, 2022
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.09 KB | None | 0 0
  1. class Result {
  2.  
  3.     /*
  4.      * Complete the 'wordBreakCount' function below.
  5.      *
  6.      * The function accepts STRING_ARRAY dictionary as parameter and the original
  7.      * string txt on which segmentation is to be performed. The function returns the
  8.      * count of all possible segmentation arrangements.
  9.      */
  10.    
  11.     public static void helper(int idx, String txt, List<String> dict, int[] dp){
  12.         if(idx == txt.length()){
  13.             return;
  14.         }
  15.         for(String word: dict){
  16.             int cSize = word.length();
  17.             if(idx+cSize > txt.length() || !word.equals(txt.substring(idx, idx+cSize))){
  18.                 continue;
  19.             }
  20.             dp[idx+cSize-1] += (idx==0)?1:dp[idx-1];
  21.             dp[idx+cSize-1] %= 1000000007;
  22.         }
  23.         helper(idx+1, txt, dict, dp);
  24.     }
  25.    
  26.     public static int wordBreakCount(List<String> dictionary, String txt) {
  27.         // Write your code here
  28.         int[] dp = new int[txt.length()+1];
  29.         Arrays.fill(dp, 0);
  30.         helper(0, txt, dictionary, dp);
  31.         return dp[txt.length()-1];
  32.     }
  33.  
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement