Skip to main content

Command Palette

Search for a command to run...

Split a String in Balanced Strings

Published
1 min read
Split a String in Balanced Strings
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.

Balanced strings are those that have an equal quantity of 'L' and 'R' characters.

Given a balanced string s, split it into some number of substrings such that:

  • Each substring is balanced.

Return the maximum number of balanced strings you can obtain.

LeetCode Problem - 1221

class Solution {
    public int balancedStringSplit(String s) {
        int answer = 0; // Initialize a counter to keep track of the number of balanced substrings
        int Lcount = 0; // Initialize a counter to keep track of the balance between 'L' and 'R'

        // Loop through the string from the end to the beginning
        for(int i = s.length() - 1; i >= 0; i--) {
            if(s.charAt(i) == 'L') {
                Lcount++; // Increment the counter if the current character is 'L'
            } else {
                Lcount--; // Decrement the counter if the current character is not 'L' (i.e., it is 'R')
            }

            // If Lcount is zero, it means we have a balanced substring
            if(Lcount == 0) {
                answer++; // Increment the count of balanced substrings
            }
        }

        return answer; // Return the total count of balanced substrings
    }
}

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.