Maximize cost of deletions to obtain string having no pair of similar adjacent characters | Java program for the above approach ; Function to find maximum cost to remove consecutive characters ;  ; Maximum cost ; Traverse from 0 to len ( s ) - 2 ; If characters are identical ; Add cost [ i ] if its maximum ; Add cost [ i + 1 ] if its maximum ; Increment i ; Return the final max count ; Driver code ; Given string s ; Given cost of removal ; Function call
import java . io . * ; import java . util . * ; class GFG { static int Maxcost ( String s , int [ ] cost ) { ' ' ▁ Initialize ▁ the ▁ count ' '' int maxcost = 0 ; int i = 0 ; while ( i < s . length ( ) - 1 ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) { if ( cost [ i ] > cost [ i + 1 ] ) maxcost += cost [ i ] ; else { maxcost += cost [ i + 1 ] ; cost [ i + 1 ] = cost [ i ] ; } } i ++ ; } return maxcost ; } public static void main ( String [ ] args ) { String s = " abaac " ; int [ ] cost = { 1 , 2 , 3 , 4 , 5 } ; System . out . print ( Maxcost ( s , cost ) ) ; } }