Advertisement
uopspop

Untitled

Feb 4th, 2021
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.71 KB | None | 0 0
  1. class Solution {
  2.     public boolean wordBreak(String s, List<String> wordDict) {
  3.        
  4.         return wordBreak_helper(s, wordDict, 0);
  5.        
  6.     }
  7.    
  8.     public boolean wordBreak_helper(String s, List<String> wordDict, int i_dict) {
  9.         if (i_dict == wordDict.size()) {
  10.             if (s.isEmpty()) return true;
  11.             return false;
  12.         }
  13.        
  14.         // use it
  15.         String word = wordDict.get(i_dict);
  16.         boolean result_use_it = wordBreak_helper(s.replaceAll(word,""), wordDict, i_dict + 1);
  17.         // not use it
  18.         boolean result_not_use_it = wordBreak_helper(s, wordDict, i_dict + 1);
  19.        
  20.         return result_use_it || result_not_use_it;
  21.        
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement