Balanced strings are those who have an equal quantity of 'L' and 'R' characters.
Given a balanced string
Return the maximum amount of split balanced strings.
Balanced strings are those who have an equal quantity of 'L' and 'R' characters.
Given a balanced string
Return the maximum amount of split balanced strings.
Solution in C++:
int balancedStringSplit(string s) {
int ret = 0;
int r_c = 0;
int l_c = 0;
for(int iter = 0; iter < s.length(); ++iter){
if(s[iter] == 'R'){
++r_c;
} else {
++l_c;
}
if(r_c == l_c){
++ret;
r_c = 0;
l_c = 0;
}
}
return ret;
}
A similar kind of problem as above but with a slight difference. In the above question, the requirement is to just count the number of L's and R's and if the count matches return the final count. But in this below question match, the pattern like set of L's and set of R's has to have the same count. Return the count such that the maximum number of matches occur.
Solution in C++:
int num_of_chars(string s, char c, int index){
int count = 0;
while(index < s.length()){
if(s[index] == c){
++count;
++index;
}else{
return count;
}
}
return count;
}
int balancedStringSplit(string s) {
int iter = 0;
int ret = 0;
int init = 0;
while(iter < s.length()){
int idx1;
int idx2;
idx1 = num_of_chars(s, s[iter], iter);
idx2 = num_of_chars(s, s[iter + idx1], iter + idx1) + idx1;
cout<<"idx1 is: "<<idx1<<" idx2 is: "<<idx2<<endl;
iter += idx1 + (idx1 > idx2) ? idx2 : idx1;
cout<<"iter value is: "<<iter<<endl;
if(idx1 > 0 && idx2 > 0){
++ret;
}
}
return ret;
}
Given a balanced string
s split it in the maximum amount of balanced strings.Return the maximum amount of split balanced strings.
Balanced strings are those who have an equal quantity of 'L' and 'R' characters.
Given a balanced string
s split it in the maximum amount of balanced strings.Return the maximum amount of split balanced strings.
Input: s = "RLRRLLRLRL" Output: 4
Solution in C++:
int balancedStringSplit(string s) {
int ret = 0;
int r_c = 0;
int l_c = 0;
for(int iter = 0; iter < s.length(); ++iter){
if(s[iter] == 'R'){
++r_c;
} else {
++l_c;
}
if(r_c == l_c){
++ret;
r_c = 0;
l_c = 0;
}
}
return ret;
}
A similar kind of problem as above but with a slight difference. In the above question, the requirement is to just count the number of L's and R's and if the count matches return the final count. But in this below question match, the pattern like set of L's and set of R's has to have the same count. Return the count such that the maximum number of matches occur.
Solution in C++:
int num_of_chars(string s, char c, int index){
int count = 0;
while(index < s.length()){
if(s[index] == c){
++count;
++index;
}else{
return count;
}
}
return count;
}
int balancedStringSplit(string s) {
int iter = 0;
int ret = 0;
int init = 0;
while(iter < s.length()){
int idx1;
int idx2;
idx1 = num_of_chars(s, s[iter], iter);
idx2 = num_of_chars(s, s[iter + idx1], iter + idx1) + idx1;
cout<<"idx1 is: "<<idx1<<" idx2 is: "<<idx2<<endl;
iter += idx1 + (idx1 > idx2) ? idx2 : idx1;
cout<<"iter value is: "<<iter<<endl;
if(idx1 > 0 && idx2 > 0){
++ret;
}
}
return ret;
}
Comments
Post a Comment