#K49397. Longest Subsequence with Exactly Two Distinct Characters
Longest Subsequence with Exactly Two Distinct Characters
Longest Subsequence with Exactly Two Distinct Characters
You are given a string s
consisting of lowercase English letters. Your task is to find the length of the longest subsequence that can be formed using exactly two distinct characters from s
.
A subsequence is derived from the string by deleting some or no characters without changing the order of the remaining characters.
If there are fewer than two distinct characters in the string, the answer should be 0
.
Note: The subsequence does not need to alternate between the two characters; all occurrences of the two selected characters (in the original order) can be taken.
Example:
Input: abcabcabc Output: 6</p>Explanation: One possible subsequence is "aaabbb" formed by taking all the occurrences of 'a' and 'b'. Its length is 6.
inputFormat
The input consists of a single line containing the string s
(with length between 0 and 105), composed of lowercase English letters.
outputFormat
Output a single integer – the length of the longest subsequence that can be formed using exactly two distinct characters. If no such subsequence exists, output 0
.
abcabcabc
6
</p>