Longest Sequence


public class LongestSubsequence{
    public static void main(String[] args){
        System.out.print("Enter a sequence of letters: ");
        String s = StdIn.readString();
        System.out.println("The longest repeating sequence is " + longestRepeatingSubsequence(s));
    }

    public static String longestRepeatingSubsequence(String s){
        int currentIndex = 0;
        int longestIndex = 0;
        int currentSize = 1;
        int maxSize = 1;
        for (int i = 1; i < s.length(); i++){
            if (s.charAt(i) == s.charAt(i - 1)){
                currentSize++;
                if (currentSize > maxSize){
                    maxSize = currentSize;
                    longestIndex = currentIndex;
                }
            } else {
            currentIndex = i + 1;
            currentSize = 1;
            }
        }
        return (s.substring(longestIndex, longestIndex + maxSize));
    }
}

/*
Use the example "abaabacccaabbbba":
The result should be "The longest repeating sequence is bbbb"
*/