Maximum Score After Splitting a String
Given a string s
of zeros and ones, return the maximum score after splitting the string into two non-empty substrings (i.e. left substring and right substring).
The score after splitting a string is the number of zeros in the left substring plus the number of ones in the right substring.
LeetCode Problem - 1422
class Solution {
public int maxScore(String s) {
int answer = 0;
for(int i=0; i<s.length(); i++){
int temp = splitChecker(i, s);
answer = Math.max(answer, temp);
}
return answer;
}
public int splitChecker(int k, String str){
int zeroCount = 0;
for(int i=0; i<=k; i++){
if(str.charAt(i) == '0') zeroCount++;
}
int oneCount = 0;
for(int i=k; i<str.length(); i++){
if(str.charAt(i) == '1') oneCount++;
}
if(zeroCount==0 || oneCount ==0){
return (zeroCount+oneCount)-1;
}
return zeroCount+oneCount;
}
}
Subscribe to my newsletter
Read articles from Gulshan Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Gulshan Kumar
Gulshan Kumar
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.