Lexicographically largest sub | Function to return the lexicographically largest sub - sequence of s ; Get the max character from the string ; Use all the occurrences of the current maximum character ; Repeat the steps for the remaining string ; Driver code
< ? php function getSubSeq ( $ s , $ n ) { $ res = " " ; $ cr = 0 ; while ( $ cr < $ n ) { $ mx = $ s [ $ cr ] ; for ( $ i = $ cr + 1 ; $ i < $ n ; $ i ++ ) $ mx = max ( $ mx , $ s [ $ i ] ) ; $ lst = $ cr ; for ( $ i = $ cr ; $ i < $ n ; $ i ++ ) if ( $ s [ $ i ] == $ mx ) { $ res . = $ s [ $ i ] ; $ lst = $ i ; } $ cr = $ lst + 1 ; } return $ res ; } $ s = " geeksforgeeks " ; $ n = strlen ( $ s ) ; echo getSubSeq ( $ s , $ n ) ; ? >