|
| 1 | +package com.interview.dynamic; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Set; |
| 5 | + |
| 6 | +public class BreakMultipleWordsWithNoSpaceIntoSpace { |
| 7 | + |
| 8 | + public String breakWord(char[] str,int low,Set<String> dictionary){ |
| 9 | + |
| 10 | + StringBuffer buff = new StringBuffer(); |
| 11 | + for(int i= low; i < str.length; i++){ |
| 12 | + buff.append(str[i]); |
| 13 | + if(dictionary.contains(buff.toString())){ |
| 14 | + String result = breakWord(str, i+1, dictionary); |
| 15 | + if(result != null){ |
| 16 | + return buff.toString() + " " + result; |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | + if(dictionary.contains(buff.toString())){ |
| 21 | + return buff.toString(); |
| 22 | + } |
| 23 | + return null; |
| 24 | + } |
| 25 | + |
| 26 | + public String dynamic(char str[], Set<String> dictionary){ |
| 27 | + |
| 28 | + int[][] T = new int[str.length][str.length]; |
| 29 | + for(int i=0; i < T.length; i++){ |
| 30 | + for(int j=0; j < T[i].length; j++){ |
| 31 | + if(i == j && dictionary.contains(str[i])){ |
| 32 | + T[i][j] = i; |
| 33 | + }else{ |
| 34 | + T[i][j] = -1; |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + StringBuffer buff = new StringBuffer(); |
| 39 | + |
| 40 | + for(int l = 2; l <= str.length; l++){ |
| 41 | + for(int i = 0; i <= str.length-l; i++){ |
| 42 | + int j = i + l-1; |
| 43 | + buff.append(str[i]); |
| 44 | + int k; |
| 45 | + for(k=i+1; k <=j; k++){ |
| 46 | + if(dictionary.contains(buff.toString()) && T[k][j] != -1){ |
| 47 | + // T[i][j] = k; |
| 48 | + break; |
| 49 | + } |
| 50 | + buff.append(str[k]); |
| 51 | + } |
| 52 | + if(dictionary.contains(buff.toString())){ |
| 53 | + T[i][j] = k; |
| 54 | + } |
| 55 | + buff.delete(0, buff.length()); |
| 56 | + } |
| 57 | + } |
| 58 | + int low =0; |
| 59 | + int high = str.length-1; |
| 60 | + StringBuffer buffer = new StringBuffer(); |
| 61 | + while(true){ |
| 62 | + int r = T[low][high]; |
| 63 | + if(r==-1){ |
| 64 | + return null; |
| 65 | + } |
| 66 | + buffer.append(get(str,low,r)).append(" "); |
| 67 | + low = r; |
| 68 | + if(low == str.length){ |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + return buffer.toString(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Easier implementation to understand |
| 77 | + * Gives preference to longer words over splits |
| 78 | + * e.g peanutbutter with dict{pea nut butter peanut} it would result in |
| 79 | + * peanut butter instead of pea nut butter. Above method does the opposite |
| 80 | + * @return |
| 81 | + */ |
| 82 | + public String breakWordDP(String word, Set<String> dict){ |
| 83 | + int T[][] = new int[word.length()][word.length()]; |
| 84 | + |
| 85 | + for(int i=0; i < T.length; i++){ |
| 86 | + for(int j=0; j < T[i].length ; j++){ |
| 87 | + T[i][j] = -1; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + for(int l = 1; l <= word.length(); l++){ |
| 92 | + for(int i=0; i < word.length() -l + 1 ; i++){ |
| 93 | + int j = i + l-1; |
| 94 | + String str = word.substring(i,j+1); |
| 95 | + if(dict.contains(str)){ |
| 96 | + T[i][j] = i; |
| 97 | + continue; |
| 98 | + } |
| 99 | + for(int k=i+1; k <= j; k++){ |
| 100 | + if(T[i][k-1] != -1 && T[k][j] != -1){ |
| 101 | + T[i][j] = k; |
| 102 | + break; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + if(T[0][word.length()-1] == -1){ |
| 108 | + return null; |
| 109 | + } |
| 110 | + StringBuffer buffer = new StringBuffer(); |
| 111 | + int i = 0; int j = word.length() -1; |
| 112 | + while(i < j){ |
| 113 | + int k = T[i][j]; |
| 114 | + if(i == k){ |
| 115 | + buffer.append(word.substring(i, j+1)); |
| 116 | + break; |
| 117 | + } |
| 118 | + buffer.append(word.substring(i,k) + " "); |
| 119 | + i = k; |
| 120 | + } |
| 121 | + |
| 122 | + return buffer.toString(); |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + private String get(char[] str,int low,int high){ |
| 127 | + StringBuffer b = new StringBuffer(); |
| 128 | + for(int i=low; i < high; i++){ |
| 129 | + b.append(str[i]); |
| 130 | + } |
| 131 | + return b.toString(); |
| 132 | + } |
| 133 | + |
| 134 | + public static void main(String args[]){ |
| 135 | + Set<String> dictionary = new HashSet<String>(); |
| 136 | + dictionary.add("I"); |
| 137 | + dictionary.add("like"); |
| 138 | + dictionary.add("had"); |
| 139 | + dictionary.add("play"); |
| 140 | + dictionary.add("to"); |
| 141 | + String str = "Ihadliketoplay"; |
| 142 | + BreakMultipleWordsWithNoSpaceIntoSpace bmw = new BreakMultipleWordsWithNoSpaceIntoSpace(); |
| 143 | + String result = bmw.dynamic(str.toCharArray(), dictionary); |
| 144 | + String result1 = bmw.breakWordDP(str, dictionary); |
| 145 | + |
| 146 | + System.out.println(result); |
| 147 | + System.out.print(result1); |
| 148 | + } |
| 149 | +} |
0 commit comments