nvprhanumantharao
BAN USERpackage Strings;
import java.util.HashSet;
import java.util.Set;
public class FindWords {
public static void main(String[] args) {
// TODO Auto-generated method stub
dictionary.add("beyond");
dictionary.add("and");
dictionary.add("bath");
dictionary.add("bed");
//dictionary.add("be");
String str = "bedbathandbeyond";
System.out.println(isValidString(str));
}
static Set<String> dictionary = new HashSet<String>();
static boolean isValidString (String s) {
if (s.length() == 0)
return false;
return wordBreak(s);
}
static boolean wordBreak(String s) {
for (int i=1; i <= s.length(); i++) {
String sub = s.substring(i, s.length());
if (dictionary.contains(s.substring(0, i)) && (sub.length()>0?wordBreak(sub):true)) {
return true;
}
}
return false;
}
}
public static String revWordsOpt(String str)
- nvprhanumantharao January 04, 2018{
String s[] =str.split(" ");
StringBuffer temp =new StringBuffer();
for (int i=s.length-1;i>=0;i--)
{
if (s[i].trim().length()>0)
temp.append(s[i]).append(" ");
}
if (temp.length()>1)
temp.setLength(temp.length()-1);
return temp.toString();
}