找出一个string中第一个重复出现的单词;
sample: he had had a good son.
output: had
句子中可能会出现,comma,colon, semicolon, dash, fullstop/dot
解法:1;dash和dot 都是regular expression中的,出现会有歧义,因此需要加上 转意符\
2, hashset 的用法,函数中的contain 和add比较好用
package lintcode; import java.util.HashSet; public class repeatword { static String firstRepeatedWord(String s) { String [] ss=s.split("[ ,;:\-\.]"); HashSet<String> store=new HashSet<String>(); for(String temp: ss){ if(temp.isEmpty()){ continue; } if(store.contains(temp)){ return temp; } else{ store.add(temp); } } return ""; } }