Longest subsequence with different adjacent characters | 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
def longestSubsequence ( s ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE answer = 0 ; NEW_LINE prev = ' - ' ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( prev != s [ i ] ) : NEW_LINE INDENT prev = s [ i ] ; NEW_LINE answer += 1 ; NEW_LINE DEDENT DEDENT return answer ; NEW_LINE DEDENT str = " ababa " ; NEW_LINE print ( longestSubsequence ( str ) ) ; NEW_LINE