Cost to make a string Panagram | Function to return the total cost required to make the string Pangram ; Mark all the alphabets that occurred in the string ; Calculate the total cost for the missing alphabets ; Driver Code
< ? php function pangramCost ( $ arr , $ str ) { $ cost = 0 ; $ occurred = array ( ) ; for ( $ i = 0 ; $ i < 26 ; $ i ++ ) $ occurred [ $ i ] = false ; for ( $ i = 0 ; $ i < strlen ( $ str ) ; $ i ++ ) { $ idx = ord ( $ str [ $ i ] ) - 97 ; $ occurred [ $ idx ] = true ; } for ( $ i = 0 ; $ i < 26 ; $ i ++ ) { if ( $ occurred [ $ i ] == false ) $ cost += $ arr [ $ i ] ; } return $ cost ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 ) ; $ str = " abcdefghijklmopqrstuvwz " ; echo pangramCost ( $ arr , $ str ) ; ? >