#C10489. Longest Non-Adjacent Subsequence Length
Longest Non-Adjacent Subsequence Length
Longest Non-Adjacent Subsequence Length
You are given a string S
consisting of lowercase English letters. Your task is to compute the length of the longest subsequence of S
in which no two consecutive characters are identical.
Formally, a subsequence is a sequence that can be derived from the string by deleting some or no characters without changing the order of the remaining characters. The objective is to choose as many characters as possible such that if you write them in order, no two adjacent characters are the same. For example:
- For
S = "aabbc"
, one optimal subsequence isabc
and its length is 3. - For
S = "aaaa"
, the longest such subsequence is justa
with length 1.
Note: The order of characters in the subsequence must be the same as their order in the original string.
inputFormat
The input consists of a single line containing the string S
(0 ≤ |S| ≤ 105), which is composed of lowercase English letters.
outputFormat
Output a single integer representing the length of the longest subsequence of S
where no two adjacent characters are the same.
aabbc
3