I wrote the following Java code, to find the intersection between the prefix and the suffix of a String in Java.
// you can also use imports, for example:
// import java.math.*;
import java.util.*;
class Solution {
    public int max_prefix_suffix(String S) {
        if (S.length() == 0) {
            return 1;
        }
        // prefix candidates
        Vector<String> prefix = new Vector<String>();
        // suffix candidates
        Vector<String> suffix = new Vector<String>();
        // will tell me the difference
        Set<String> set = new HashSet<String>();
        int size = S.length();
        for (int i = 0; i < size; i++) {
            String candidate = getPrefix(S, i);
            // System.out.println( candidate );
            prefix.add(candidate);
        }
        for (int i = size; i >= 0; i--) {
            String candidate = getSuffix(S, i);
            // System.out.println( candidate );
            suffix.add(candidate);
        }
        int p = prefix.size();
        int s = suffix.size();
        for (int i = 0; i < p; i++) {
            set.add(prefix.get(i));
        }
        for (int i = 0; i < s; i++) {
            set.add(suffix.get(i));
        }
        System.out.println("set: " + set.size());
        System.out.println("P: " + p + " S: " + s);
        int max = (p + s) - set.size();
        return max;
    }
    // codility
    // y t i l i d o c
    public String getSuffix(String S, int index) {
        String suffix = "";
        int size = S.length();
        for (int i = size - 1; i >= index; i--) {
            suffix += S.charAt(i);
        }
        return suffix;
    }
    public String getPrefix(String S, int index) {
        String prefix = "";
        for (int i = 0; i <= index; i++) {
            prefix += S.charAt(i);
        }
        return prefix;
    }
    public static void main(String[] args) {
        Solution sol = new Solution();
        String t1 = "";
        String t2 = "abbabba";
        String t3 = "codility";
        System.out.println(sol.max_prefix_suffix(t1));
        System.out.println(sol.max_prefix_suffix(t2));
        System.out.println(sol.max_prefix_suffix(t3));
        System.exit(0);
    }
}
Some test cases are:
String t1 = "";
String t2 = "abbabba";
String t3 = "codility";
and the expected values are:
1, 4, 0
My idea was to produce the prefix candidates and push them into a vector, then find the suffix candidates and push them into a vector, finally push both vectors into a Set and then calculate the difference. However, I'm getting 1, 7, and 0. Could someone please help me figure it out what I'm doing wrong?
 
     
     
     
     
     
     
    