Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- public class TaskOne {
- public static void main(String[] args) {
- solution("zyzyzyz");
- }
- public static int solution(String S) {
- List<Integer> wordsLength = new ArrayList<>();
- boolean foundUnique = false;
- for(int j=1; j<S.length(); j++) {
- for (int i = 0; i < S.length()-j; i++) {
- String tempWord = S;
- String subword = S.substring(i, i+j);
- if(isMoreThanOneOccurence(S, subword)){
- wordsLength.add(subword.length());
- }
- }
- }
- if(wordsLength.isEmpty()) {
- return S.length();
- }
- return Collections.min(wordsLength);
- }
- public static boolean isMoreThanOneOccurence(String s, String subword) {
- int counter = 0;
- for(int i=0; i<=s.length()-subword.length();i++) {
- if(s.substring(i, i+subword.length()).equals(subword)) {
- counter++;
- }
- }
- return counter<2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement