fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. class Ideone {
  9. public static void main(String[] args) {
  10. Ideone m = new Ideone();
  11.  
  12. // Test cases
  13. String input1 = "goooooogle";
  14. String input2 = "abccccd";
  15.  
  16. System.out.println("Input: " + input1);
  17. System.out.println("Phrases: " + m.getPhrases(input1));
  18.  
  19. System.out.println("Input: " + input2);
  20. System.out.println("Phrases: " + m.getPhrases(input2));
  21. }
  22.  
  23. public List<String> getPhrases(String s) {
  24. List<String> res = new ArrayList<>();
  25. Set<String> set = new HashSet<>();
  26.  
  27. for (int i = 0; i < s.length(); i++) {
  28. String curr = "" + s.charAt(i);
  29. if (set.contains(curr)) {
  30. int j = i;
  31. curr = "" + s.charAt(i);
  32. while (j + 1 < s.length() && set.contains(curr)) {
  33. j++;
  34. curr = s.substring(i, j + 1);
  35. }
  36. i = j;
  37. }
  38. set.add(curr);
  39. res.add(curr);
  40. }
  41.  
  42. return res;
  43. }
  44. }
  45.  
Success #stdin #stdout 0.16s 57580KB
stdin
Standard input is empty
stdout
Input: goooooogle
Phrases: [g, o, oo, ooo, gl, e]
Input: abccccd
Phrases: [a, b, c, cc, cd]