Created
April 19, 2016 18:44
-
-
Save deyindra/e9231bc16deda9de067599cbb109b445 to your computer and use it in GitHub Desktop.
Word Breaking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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