san4you88
BAN USERimport java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
public class StringMatcher {
HashSet<String> stringSet = new HashSet<String>();
void setup(String[] words){
if(words != null){
Collections.addAll(stringSet, words);
}
}
boolean isMatch(String word) {
if(stringSet.contains(word)){
return true;
}
Iterator<String> stringSetiIterator = stringSet.iterator();
while(stringSetiIterator.hasNext()){
String current = stringSetiIterator.next();
if(current.length() == word.length()){
boolean matched = true;
for(int i = 0,j =word.length();i< j;i++ ){
if(!(current.charAt(i) == word.charAt(i) || current.charAt(i) == '.' || word.charAt(i) == '.')){
matched = false;
break;
}
}
if(matched){
return true;
}
}
}
return false;
}
public static void main(String[] args) {
String[] dict = { "hello", "mi." };
StringMatcher list = new StringMatcher();
list.setup(dict);
String[] words = {"hello", ".ell.", "hel.a", "hel.", "mix"};
for (String w : words) {
System.out.println(w + " : " + list.isMatch(w));
}
}
}
- san4you88 March 17, 2014