pamit
BAN USER// Find the number of occurrences of a particular word.
public static int countOccurance(String input, String word, int count) {
if (input == null || word == null || input.isEmpty() || word.isEmpty())
return count;
if (input.length() < word.length())
return count;
String subString = input.substring(0, word.length());
if (subString.equals(word))
return countOccurance(input.substring(word.length()), word, count + 1);
else
return countOccurance(input.substring(1), word, count);
}
// Find the smallest sentence containing a set of words.
public static String smallestSentence(String input, String output, String... words) {
if (input == null || input.isEmpty() || words == null || words.length == 0)
return output;
int startIndex = -1;
int endIndex = -1;
for (String word : words) {
int indexOf = input.indexOf(word);
if (indexOf == -1)
return output;
startIndex = (startIndex < indexOf && startIndex != -1) ? startIndex : indexOf;
endIndex = endIndex > indexOf + word.length() ? endIndex : indexOf + word.length();
}
output = (output.length() < (endIndex - startIndex) && output.length() != 0) ? output : input.substring(startIndex, endIndex);
return smallestSentence(input.substring(endIndex), output, words);
}
Can you provide me your testing inputs ?
- pamit July 04, 2012