Skip to content

Instantly share code, notes, and snippets.

@cAstraea
Created February 6, 2017 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cAstraea/222af59643b48922dd3865741d9cf25e to your computer and use it in GitHub Desktop.
Save cAstraea/222af59643b48922dd3865741d9cf25e to your computer and use it in GitHub Desktop.
break string into two segments if found in dictionary
function breakWord(stringToBreak, dictionary) {
for (var i = 1; i <= stringToBreak.length; i++) {
var prefix = stringToBreak.substring(0, i);
if(dictionary.has(prefix)) {
var suffix = stringToBreak.substring(i, stringToBreak.length);
if(dictionary.has(suffix)) {
return prefix + ' ' + suffix;
}
}
}
return null;
}
var mySet = new Set();
mySet.add("huge");
mySet.add("breakthrough");
var inputString = "hugebreakthrough";
var result = breakWord(inputString, mySet);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment