Longest subsequence with different adjacent characters | C ++ program for the above approach ; Function to find the longest Subsequence with different adjacent character ; Length of the string s ; Previously picked character ; If the current character is different from the previous then include this character and update previous character ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int longestSubsequence ( string s ) { int n = s . length ( ) ; int answer = 0 ; char prev = ' - ' ; for ( int i = 0 ; i < n ; i ++ ) { if ( prev != s [ i ] ) { prev = s [ i ] ; answer ++ ; } } return answer ; } int main ( ) { string str = " ababa " ; cout << longestSubsequence ( str ) ; return 0 ; }