You are given a string that contains characters. Your task is to determine the maximum possible size of the subsequence of such that no two adjacent characters in 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