Skip to content

Instantly share code, notes, and snippets.

@deyindra
Created April 19, 2016 18:44
Show Gist options
  • Save deyindra/e9231bc16deda9de067599cbb109b445 to your computer and use it in GitHub Desktop.
Save deyindra/e9231bc16deda9de067599cbb109b445 to your computer and use it in GitHub Desktop.
Word Breaking
public static List<String> wordBreak(String str){
if(str==null){
throw new IllegalArgumentException("Invalid String");
}
List<String> wordList = new ArrayList<>();
String subString="";
boolean isQuoteFound=false;
for(char c:str.toCharArray()){
if(c!=' ' && c!='"'){
subString+=c;
}else if(c=='"'){
subString+=c;
isQuoteFound=!isQuoteFound;
}else{
if(isQuoteFound){
subString+=c;
}else{
if(!"".equals(subString)){
wordList.add(subString);
subString="";
isQuoteFound=false;
}
}
}
}
if(!"".equals(subString)) {
wordList.add(subString);
}
return wordList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment