To store counts of different characters
$ count = array_fill ( 0 , 26 , 0 ) ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ count [ ord ( $ str [ $ i ] ) - 97 ] == 0 ) $ dist_count ++ ; $ count [ ord ( $ str [ $ i ] ) - 97 ] ++ ; }