Skip to content

Instantly share code, notes, and snippets.

@mgarod
Created November 13, 2016 22:48
Show Gist options
  • Save mgarod/468c339fb1c5227702f80112c74e7ac2 to your computer and use it in GitHub Desktop.
Save mgarod/468c339fb1c5227702f80112c74e7ac2 to your computer and use it in GitHub Desktop.
static String firstRepeatedWord(String s) {
HashSet<String> hashset = new HashSet<>();
String[] splited = s.split("\\s+");
for (String word : splited) {
word = word.trim().replaceAll("\\W", "");
if (hashset.contains(word)) {
return word;
} else {
hashset.add(word);
}
}
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment