fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int max_balanced_substrings(const char *s) {
  5. int countX = 0, countY = 0;
  6. int maxBalancedCount = 0;
  7. int length = strlen(s);
  8.  
  9. for (int i = 0; i < length; i++) {
  10. if (s[i] == 'X') {
  11. countX++;
  12. } else if (s[i] == 'Y') {
  13. countY++;
  14. }
  15.  
  16. // Check if the current substring is balanced
  17. if (countX == countY) {
  18. maxBalancedCount++;
  19. // Reset counters for the next substring
  20. countX = 0;
  21. countY = 0;
  22. }
  23. }
  24.  
  25. return maxBalancedCount;
  26. }
  27.  
  28. int main() {
  29. const char *input = "XXYYXY";
  30. printf("%d\n", max_balanced_substrings(input)); // Output: 2
  31. return 0;
  32. }
Success #stdin #stdout 0s 5292KB
stdin
XXYYXY
stdout
2