string

how to get the length of repeated substring in case it exists


Q: Write a java method to to identify if a given text string contains the same repeated substring and return the length of the substring or zero if no repeated substring exists.

The first part is solved by the answers in

but I also need the length of the same repeated substring. The length of the repeated substring is also called period. E.g. in case of text string "abcabcabcabcabc" the period is equal to 3 (length of "abc").


Solution

  • A: Using Z-function , the method with complexity O(n) that returns the length of the same repeated substring or zero in case it does not exist is the following:

        public int getPeriodicSubstringLength(String string) {
            int n = string.length();
            int[] z = z_function(string);
            for (int i = 1; i < n; i++)
                if ((n % i == 0) && (i + z[i] == n))
                    return i;
            return 0;//no repetition found
        }
    
        private int[] z_function(String string) {//z algorithm
            int n = string.length();
            int[] z = new int[n];
            int l = 0, r = 0;
            for (int i = 1; i < n; i++) {
                if (i < r)
                    z[i] = Math.min(r - i, z[i - l]);
                while (i + z[i] < n && string.charAt(z[i]) == string.charAt(i + z[i]))
                    z[i]++;
                if (i + z[i] > r) {
                    l = i;
                    r = i + z[i];
                }
            }
            return z;
        }
    

    Examples of returned values:

    getPeriodicSubstringLength("alfaalfa") --> 4
    getPeriodicSubstringLength("abababab") --> 2
    getPeriodicSubstringLength("abababac") --> 0