How to detect duplicate words from a String in Java?
问题 What are the ways by which duplicate word in a String can be detected? e.g. "this is a test message for duplicate test" contains one duplicate word test. Here, the objective is to detect all duplicate words which occur in a String. Use of regular expression is preferable to achieve the goal. 回答1: The best you can do with regexes is O(N^2) search complexity. You can easily achieve O(N) time and space search complexity by splitting the input into words and using a HashSet to detect duplicates.