Created
February 6, 2017 12:20
-
-
Save cAstraea/222af59643b48922dd3865741d9cf25e to your computer and use it in GitHub Desktop.
break string into two segments if found in dictionary
This file contains 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
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