public class FindInstrumentName {
public static void main(String[] args) {
String[] inputs = { "dog", "cat", "pig" };
String text = "a dogkcat pig playcat,pigdone";
Map<Integer, String> test = findInstruments(inputs, text);
Set<Integer> resSet = test.keySet();
Iterator it = resSet.iterator();
System.out.println(text.length());
while (it.hasNext()) {
int curIndex = (Integer) it.next();
System.out.println(curIndex + "->" + test.get(curIndex));
}
}
private static Map<Integer, String> findInstruments(String[] dict,
String text) {
Map<Integer, String> result = new HashMap<Integer, String>();
for (int i = 0; i < dict.length; i++) {
findInstrumentOps(dict[i], text, result);
}
return result;
}
private static void findInstrumentOps(String word, String text,
Map<Integer, String> result) {
for (int i = 0; i <= text.length() - word.length(); i++) {
int j = 0;
int index = i;
while (j < word.length()) {
if (!(isValiedCharacter(text.charAt(i)))) {
i++;
continue;
}
if (text.charAt(i) != word.charAt(j)) {
break;
}
if (j == word.length() - 1) {
result.put(index, word);
}
i++;
j++;
}
}
}
private static boolean isValiedCharacter(char c) {
if (c <= 'z' && c >= 'a') {
return true;
}
return false;
}
}
没有评论:
发表评论