Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. public static List<String> split(String text, Set<String> dict) {
  2.         List<String> words = splitHelper(text, dict);
  3.         List<String> finalRes = new ArrayList<>();
  4.  
  5.         for (String word : words) {
  6.             String[] tempRes = word.split(" ");
  7.             for (String sp : tempRes) {
  8.                 finalRes.add(sp);
  9.             }
  10.             break;
  11.         }
  12.  
  13.         return finalRes;
  14.  
  15.     }
  16.    
  17.     public static List<String> splitHelper(String s, Set<String> wordDict) {
  18.         ArrayList<String> [] pos = new ArrayList[s.length()+1];
  19.         pos[0]=new ArrayList<String>();
  20.         System.out.println("Position_Value-> "+pos.length);
  21.      
  22.         for(int i=0; i<s.length(); i++){
  23.             System.out.println("Outside_Position_Data-> "+pos[i] + " <-i->"+i);
  24.             if(pos[i]!=null){
  25.                 System.out.println("Position_Data-> "+pos[i] + " <-i->"+i);
  26.                 for(int j=i+1; j<=s.length(); j++){
  27.                     String sub = s.substring(i,j);
  28.                     System.out.println("Position_sub_data-> "+sub +" <-j->"+j);
  29.                     if(wordDict.contains(sub)){
  30.                         if(pos[j]==null){
  31.                             ArrayList<String> list = new ArrayList<String>();
  32.                             list.add(sub);
  33.                             pos[j]=list;
  34.                             System.out.println("Added_Content-> "+pos[j]);
  35.                         }else{
  36.                             pos[j].add(sub);
  37.                             System.out.println("Else Added_Content-> "+pos[j]);
  38.                         }
  39.      
  40.                     }
  41.                 }
  42.             }
  43.         }
  44.      
  45.         if(pos[s.length()]==null){
  46.             System.out.println("last value:"+s.length());
  47.             return new ArrayList<String>();
  48.         }else{
  49.             ArrayList<String> result = new ArrayList<String>();
  50.             System.out.println("last value data ->"+Arrays.toString(pos) +" //Length+"+s.length());
  51.             dfs(pos, result, "", s.length());
  52.             return result;
  53.         }
  54.     }
  55.      
  56.     public static void dfs(ArrayList<String> [] pos, ArrayList<String> result, String curr, int i){
  57.         System.out.println("DFS_INDEX->"+i+ "//->"+Arrays.toString(pos));
  58.         if(i==0){
  59.             result.add(curr.trim());
  60.             System.out.println("DFS_INDEX->"+i+ "//->"+result);
  61.             return;
  62.         }
  63.         System.out.println("DFS_DATA->"+pos[i]);
  64.         for(String s: pos[i]){
  65.             String combined = s + " "+ curr;
  66.             dfs(pos, result, combined, i-s.length());
  67.         }
  68.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement