Advertisement
MuzammiL5

Min Flips to make alternate 0's & 1's

Jul 11th, 2021
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.38 KB | None | 0 0
  1. char flip(char ch) {
  2. return ( ch == '0') ? '1' : '0';
  3. }
  4.  
  5. int minFlips(string str, char expected) {
  6. int count = 0;
  7. for (int i=0; i<str.length(); i++) {
  8. if (str[i] != expected) {
  9. count ++;
  10. }
  11. expected = flip(expected);
  12. }
  13. return count;
  14. }
  15.  
  16. int minFlips (string S)
  17. {
  18. return min(minFlips(S, '1'), minFlips(S, '0'));
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement