montwell
BAN USERDICTIONARY does not actually exist.
public void findWordsInScrambledString(String scrambled) {
String scrambledToLower = scrambled.toLowerCase();
Queue<Character> charQueue = new LinkedList<Character>();
for(int i = 0; i < scrambledToLower.length(); i++) {
charQueue.add(scrambledToLower.charAt(i));
}
String x = "";
while(!charQueue.isEmpty()) {
x = x + charQueue.remove();
String word = getWord(x);
if(word != null) {
System.out.print(word + " ");
x = "";
}
}
}
public String getWord(String possibleWord) {
if(containsVowel(possibleWord)) {
return getWordFromPermutations(possibleWord);
}
return null;
}
public boolean containsVowel(String possibleWord) {
for(int i = 0; i < possibleWord.length(); i++) {
char current = possibleWord.charAt(i);
if(current == 'a' || current == 'e' || current == 'i' || current == 'o' || current == 'u'
|| current == 'y') {
return true;
}
}
return false;
}
public String getWordFromPermutations(String possibleWord) {
char[] characters = new char[possibleWord.length()];
for(int i = 0; i < possibleWord.length(); i++) {
characters[i] = possibleWord.charAt(i);
}
return getWordFromPermutations(characters, possibleWord.length());
}
public String getWordFromPermutations(char[] characters, int height) {
String word = null;
if(height == 1) {
String possibleWord = new String(characters);
if(DICTIONARY.contains(possibleWord)) {
word = possibleWord;
}
return word;
}
for(int i = 0; i < height; i++) {
swap(characters, i, height - 1);
word = getWordFromPermutations(characters, height - 1);
if(word != null) return word;
swap(characters, i, height - 1);
}
return word;
}
public void swap(char[] a, int i, int j) {
char temp = a[i];
a[i] = a[j];
a[j] = a[i];
}
public void getPaths(TreeNode root, List<String> parentPaths) {
List<String> childPaths = new LinkedList<String>();
if(root.left == null && root.right == null) {
parentPaths.add(root.val + "");
}
if(root.left != null) {
getPaths(root.left, childPaths);
}
if(root.right != null) {
getPaths(root.right, childPaths);
}
for(String childPath : childPaths) {
parentPaths.add(root.val + "->" + childPath);
}
}
- montwell November 06, 2016