Unique subsequences

You are given a string S that contains N characters. Your task is to determine the maximum possible size of the subsequence T of S such that no two adjacent characters in T are the same.

Note: The string contains only lowercase English alphabets.

Example:

Input:  n=5, str="ababa"
Output: 5

Approach

Java

public class UniqueSubsequences {
    public static void main(String args[]) throws Exception {
        int n = 5;
        char[] a = "ababa".toCharArray();
        int c = 1;
        for (int i = 1; i < n; i++)
            if (a[i] != a[i - 1])
                c++;
        System.out.println(c);

    }
}


No comments:

Post a Comment