Skip to main content

Command Palette

Search for a command to run...

Minimum Changes to Make Alternating Binary String.

Published
2 min read
Minimum Changes to Make Alternating Binary String.
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Q - You are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa.

The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alternating, while the string "0100" is not.

Return the minimum number of operations needed to make s alternating.

LeetCode Problem: Link | Click Here

class Solution {
    public int minOperations(String s) {
        int operationForZero = 0; // Represents the count of operations needed to make the string start with '0' pattern.
        int operationForOne = 0; // Represents the count of operations needed to make the string start with '1' pattern.

        // Loop through the string characters
        for (int i = 0; i < s.length(); i++) {
            // Check for even positions in the string
            if (i % 2 == 0) {
                // If the character is '0' at even position, count operationForZero
                if (s.charAt(i) == '0') {
                    operationForZero++; // Count operations for '0' at even positions
                } else {
                    operationForOne++; // Count operations for '1' at even positions
                }
            } else { // Odd positions in the string
                // If the character is '1' at odd position, count operationForZero
                if (s.charAt(i) == '1') {
                    operationForZero++; // Count operations for '1' at odd positions
                } else {
                    operationForOne++; // Count operations for '0' at odd positions
                }
            }
        }

        // Return the minimum operations required to make the string uniform
        return Math.min(operationForZero, operationForOne);
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.