Check whether two strings contain same characters in same order | Java implementation of the approach ; String class has a constructor that allows us to specify size of String as first parameter and character to be filled in given size as second parameter . ; Function that returns true if the given Strings contain same characters in same order ; Get the first character of both Strings ; Now if there are adjacent similar character remove that character from s1 ; Now if there are adjacent similar character remove that character from s2 ; If both the Strings are equal then return true ; Driver code
class temp { static String getString ( char x ) { String s = String . valueOf ( x ) ; return s ; } static boolean solve ( String s1 , String s2 ) { String a = getString ( s1 . charAt ( 0 ) ) , b = getString ( s2 . charAt ( 0 ) ) ; for ( int i = 1 ; i < s1 . length ( ) ; i ++ ) if ( s1 . charAt ( i ) != s1 . charAt ( i - 1 ) ) { a += getString ( s1 . charAt ( i ) ) ; } for ( int i = 1 ; i < s2 . length ( ) ; i ++ ) if ( s2 . charAt ( i ) != s2 . charAt ( i - 1 ) ) { b += getString ( s2 . charAt ( i ) ) ; } if ( a . equals ( b ) ) return true ; return false ; } public static void main ( String [ ] args ) { String s1 = " Geeks " , s2 = " Geks " ; if ( solve ( s1 , s2 ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }