Return previous element in an expanding matrix | Returns left of str in an expanding matrix of a , b , c and d . ; Start from rightmost position ; If the current character is b or d , change to a or c respectively and break the loop ; If the current character is a or c , change it to b or d respectively ; Driver Code
< ? php function findLeft ( $ str ) { $ n = strlen ( $ str ) ; while ( $ n -- ) { if ( $ str [ $ n ] == ' d ' ) { $ str [ $ n ] = ' c ' ; break ; } if ( $ str [ $ n ] == ' b ' ) { $ str [ $ n ] = ' a ' ; break ; } if ( $ str [ $ n ] == ' a ' ) $ str [ $ n ] = ' b ' ; else if ( $ str [ $ n ] == ' c ' ) $ str [ $ n ] = ' d ' ; } return $ str ; } $ str = " aacbddc " ; echo " Left ▁ of ▁ " . $ str . " ▁ is ▁ " . findLeft ( $ str ) ; return 0 ; ? >