Advertisement
Dp1342

Z1

Mar 30th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6.  
  7. public class TaskOne {
  8. public static void main(String[] args) {
  9. solution("zyzyzyz");
  10. }
  11. public static int solution(String S) {
  12. List<Integer> wordsLength = new ArrayList<>();
  13. boolean foundUnique = false;
  14. for(int j=1; j<S.length(); j++) {
  15. for (int i = 0; i < S.length()-j; i++) {
  16. String tempWord = S;
  17. String subword = S.substring(i, i+j);
  18. if(isMoreThanOneOccurence(S, subword)){
  19. wordsLength.add(subword.length());
  20. }
  21. }
  22. }
  23. if(wordsLength.isEmpty()) {
  24. return S.length();
  25. }
  26. return Collections.min(wordsLength);
  27. }
  28. public static boolean isMoreThanOneOccurence(String s, String subword) {
  29. int counter = 0;
  30. for(int i=0; i<=s.length()-subword.length();i++) {
  31. if(s.substring(i, i+subword.length()).equals(subword)) {
  32. counter++;
  33. }
  34. }
  35. return counter<2;
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement