Minimum characters to be replaced to make a string concatenation of a K | Function to add an edge to graph ; Function to perform DFS traversal on the graph recursively from a given vertex u ; Visit the current vertex ; Total number of nodes in this component ; Increment the frequency of u ; Function for finding the minimum number changes required in given String ; Form the edges according to the given conditions ; Find minimum number of operations ; Frequency array for finding the most frequent character ; Frequency array for finding the most frequent character ; Finding most frequent character ; Change rest of the characters to most frequent one ; Print total number of changes ; Driver code ; Function Call
function addEdge ( adj , u , v ) { adj [ u ] . push ( v ) ; adj [ v ] . push ( u ) ; } let cnt = 0 ; let visited ; function DFS ( u , adj , fre , S ) { visited [ u ] = true ; cnt ++ ; fre [ S [ u ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; for ( let i = 0 ; i < adj [ u ] . length ; i ++ ) { if ( ! visited [ adj [ u ] [ i ] ] ) { DFS ( adj [ u ] [ i ] , adj , fre , S ) ; } } } function minimumOperations ( S , m ) { let V = 100 ; let adj = [ ] ; for ( let i = 0 ; i < V ; i ++ ) { adj . push ( [ ] ) ; } let total = 0 , N = S . length ; for ( let i = 0 ; i < adj . length ; i ++ ) adj [ i ] = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { addEdge ( adj , i , N - i - 1 ) ; addEdge ( adj , N - i - 1 , i ) ; } for ( let i = 0 ; i < N - m ; i ++ ) { addEdge ( adj , i , i + m ) ; addEdge ( adj , i + m , i ) ; } visited = new Array ( V ) ; visited . fill ( false ) ; for ( let i = 0 ; i < N ; i ++ ) { if ( ! visited [ i ] ) { let fre = new Array ( 26 ) ; fre . fill ( 0 ) ; cnt = 0 ; let maxx = - 1 ; DFS ( i , adj , fre , S ) ; for ( let j = 0 ; j < 26 ; j ++ ) maxx = Math . max ( maxx , fre [ j ] ) ; total += cnt - maxx ; } } document . write ( total ) ; } let S = " " ; let K = 2 ; minimumOperations ( S , K ) ;