First challenge is very easy:
Find first repeat word in a sentence. white space characters are ;,:,-,.,space and tab.
I failed one test case and guess it is because I used \s for space and tab. but it includes more characters.
The reason I used \\s is because I could not make it work with space and \\t the obvious right specification.
Later I realised the seemingly right regular expression failed due to forgot to escape the range letter hyphen (-) in []. It is expecting a valid range inside [] if you do not escape it. And unluckily I defined those characters after hypen :(
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package string; import java.util.HashSet; import java.util.Set; public class FirstRepeatedWord { static String getFirstRepeatedWord(String s) { if (s == null) return null; if (s.length() == 0) return ""; String[] words = s.split("[\\.;,:\\- \\t]+"); // String[] words = s.split("[\\.[;,:-] \\t]+"); Set<String> aSet = new HashSet<String>(); for (String w : words) { if (!aSet.add(w)) return w; } return ""; } public static void main(String[] args) { System.out .println(getFirstRepeatedWord("abc def,. cde-again\ttab\ttab")); } } |
No comments:
Post a Comment