Sanket and strings

I CANT UNDERSTAND WHY ARE WE USING LINE NO 15 to 17 in this code

Hi @soul_coder
temp - ā€˜aā€™ this is used to convert temp which is a char value into int value as index can be int only. Suppose if temp=ā€˜cā€™ then temp-ā€˜aā€™ will result 2. And same logic is used in line 17 since str[left] is a char variable.

also can you plz tell me why are we using the if condition?

Hi @soul_coder
See in the if condition we are doing 2 things and reasons to do that are :

  1. We are taking minimum count to find the character(either a or b) which has occurred less no of times till that particular iteration. Because we would change the occurrence of only that character which are in minority, to determine the maximum value.
  2. We are comparing the minimum value with k because we can make k changes at max. If, the minimum count is greater than k , then it means that we cannot swap all the occurrence of the character that has lesser count.
    Thus, we increment l.