Q:

Trouble with finding a substring in a string

I need to print all words which appear more once in a string.
example
input:s = "Java Ruby PHP.  Java is good. PHP please looks at Java"

output:
Java
PHP

A:

    Set<String> words = new HashSet<>();
    String str = "Java Ruby PHP.  Java is good. PHP please looks at Java";
    Matcher mat = Pattern.compile("\\b(\\w+)(?=\\b.*\\1)").matcher(str);
    while(mat.find()){
        words.add(mat.group(1));
    }
    System.out.println(words);

Also you can just split string by words and calculate count using Map. And then filter words with count > 1.

