A string is balanced if it consists of exactly two different characters and both of those characters appear exactly the same number of times. For example: "aabbab" is balanced (both 'a' and 'b' occur three times) but "aabba" is not balanced ('a' occurs three times, 'b' occurs two times). String "aabbcc" is also not balanced (it contains three different letters).
A substring of string S is a string that consists of consecutive letters in S. For example: "ompu" is a substring of "computer" but "cmptr" is not.
Write a function solution that, given a string S, returns the length of the longest balanced substring of S.
Examples:
1. Given S = "cabbacc", the function should return 4 ("abba" is the longest balanced substring).
2. Given S = "abababa", the function should return 6 ("ababab" and "bababa" are the longest balanced substrings).
3. Given S = "aaaaaaa", the function should return 0 (S does not contain a balanced substring).
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- string S is made only of lowercase letters (a−z).
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
int max=0;
for(int i=0;i<S.length();i++){
for(int j=S.length();j>0 && j>i;j--){
String sub=S.substring(i, j);
if(sub.length()%2==0 && sub.length()>max){
boolean b= testBalance(sub);
if(b){
System.out.println(i+" "+j+" char count "+sub.chars().count() +" "+sub);
if(sub.length()>max){
max=sub.length();
}
}
}
}
}
System.out.println(max);
return max;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
int max=0;
for(int i=0;i<S.length();i++){
for(int j=S.length();j>0 && j>i;j--){
String sub=S.substring(i, j);
if(sub.length()%2==0 && sub.length()>max){
boolean b= testBalance(sub);
if(b){
System.out.println(i+" "+j+" char count "+sub.chars().count() +" "+sub);
if(sub.length()>max){
max=sub.length();
}
}
}
}
}
System.out.println(max);
return max;
}
private boolean testBalance(String sub) {
Map<Character,Integer> ch= new HashMap<>();
for(int i=0;i<sub.length();i++){
if(ch.containsKey(sub.charAt(i))){
Integer k=ch.get(sub.charAt(i));
k++;
ch.put(sub.charAt(i), k);
}else{
ch.put(sub.charAt(i), 1);
}
}
boolean flag=false;
if(ch.size()==2){
Set<Integer> values = new HashSet<Integer>(ch.values());
flag = values.size() == 1;
}
return flag;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
int max=0;
for(int i=0;i<S.length();i++){
for(int j=S.length();j>0 && j>i;j--){
String sub=S.substring(i, j);
if(sub.length()%2==0 && sub.length()>max){
boolean b= testBalance(sub);
if(b){
System.out.println(i+" "+j+" char count "+sub.chars().count() +" "+sub);
if(sub.length()>max){
max=sub.length();
}
}
}
}
}
System.out.println(max);
return max;
}
private boolean testBalance(String sub) {
Map<Character,Integer> ch= new HashMap<>();
for(int i=0;i<sub.length();i++){
if(ch.containsKey(sub.charAt(i))){
Integer k=ch.get(sub.charAt(i));
k++;
ch.put(sub.charAt(i), k);
}else{
ch.put(sub.charAt(i), 1);
}
}
boolean flag=false;
if(ch.size()==2){
Set<Integer> values = new HashSet<Integer>(ch.values());
flag = values.size() == 1;
}
return flag;
}
}
Solution.java:32: error: cannot find symbol Map<Character,Integer> ch= new HashMap<>(); ^ symbol: class Map location: class Solution Solution.java:32: error: cannot find symbol Map<Character,Integer> ch= new HashMap<>(); ^ symbol: class HashMap location: class Solution Solution.java:44: error: cannot find symbol Set<Integer> values = new HashSet<Integer>(ch.values()); ^ symbol: class Set location: class Solution Solution.java:44: error: cannot find symbol Set<Integer> values = new HashSet<Integer>(ch.values()); ^ symbol: class HashSet location: class Solution 4 errors
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
int max=0;
for(int i=0;i<S.length();i++){
for(int j=S.length();j>0 && j>i;j--){
String sub=S.substring(i, j);
if(sub.length()%2==0 && sub.length()>max){
boolean b= testBalance(sub);
if(b){
System.out.println(i+" "+j+" char count "+sub.chars().count() +" "+sub);
if(sub.length()>max){
max=sub.length();
}
}
}
}
}
System.out.println(max);
return max;
}
private boolean testBalance(String sub) {
Map<Character,Integer> ch= new HashMap<>();
for(int i=0;i<sub.length();i++){
if(ch.containsKey(sub.charAt(i))){
Integer k=ch.get(sub.charAt(i));
k++;
ch.put(sub.charAt(i), k);
}else{
ch.put(sub.charAt(i), 1);
}
}
boolean flag=false;
if(ch.size()==2){
Set<Integer> values = new HashSet<Integer>(ch.values());
flag = values.size() == 1;
}
return flag;
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
int max=0;
for(int i=0;i<S.length();i++){
for(int j=S.length();j>0 && j>i;j--){
String sub=S.substring(i, j);
if(sub.length()%2==0 && sub.length()>max){
boolean b= testBalance(sub);
if(b){
System.out.println(i+" "+j+" char count "+sub.chars().count() +" "+sub);
if(sub.length()>max){
max=sub.length();
}
}
}
}
}
System.out.println(max);
return max;
}
private boolean testBalance(String sub) {
Map<Character,Integer> ch= new HashMap<>();
for(int i=0;i<sub.length();i++){
if(ch.containsKey(sub.charAt(i))){
Integer k=ch.get(sub.charAt(i));
k++;
ch.put(sub.charAt(i), k);
}else{
ch.put(sub.charAt(i), 1);
}
}
boolean flag=false;
if(ch.size()==2){
Set<Integer> values = new HashSet<Integer>(ch.values());
flag = values.size() == 1;
}
return flag;
}
}
0 2 char count 2 ca 1 5 char count 4 abba 4
0 6 char count 6 ababab 6
0
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
int max=0;
for(int i=0;i<S.length();i++){
for(int j=S.length();j>0 && j>i;j--){
String sub=S.substring(i, j);
if(sub.length()%2==0 && sub.length()>max){
boolean b= testBalance(sub);
if(b){
//System.out.println(i+" "+j+" char count "+sub.chars().count() +" "+sub);
if(sub.length()>max){
max=sub.length();
}
}
}
}
}
System.out.println(max);
return max;
}
private boolean testBalance(String sub) {
Map<Character,Integer> ch= new HashMap<>();
for(int i=0;i<sub.length();i++){
if(ch.containsKey(sub.charAt(i))){
Integer k=ch.get(sub.charAt(i));
k++;
ch.put(sub.charAt(i), k);
}else{
ch.put(sub.charAt(i), 1);
}
}
boolean flag=false;
if(ch.size()==2){
Set<Integer> values = new HashSet<Integer>(ch.values());
flag = values.size() == 1;
}
return flag;
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
int max=0;
for(int i=0;i<S.length();i++){
for(int j=S.length();j>0 && j>i;j--){
String sub=S.substring(i, j);
if(sub.length()%2==0 && sub.length()>max){
boolean b= testBalance(sub);
if(b){
//System.out.println(i+" "+j+" char count "+sub.chars().count() +" "+sub);
if(sub.length()>max){
max=sub.length();
}
}
}
}
}
// System.out.println(max);
return max;
}
private boolean testBalance(String sub) {
Map<Character,Integer> ch= new HashMap<>();
for(int i=0;i<sub.length();i++){
if(ch.containsKey(sub.charAt(i))){
Integer k=ch.get(sub.charAt(i));
k++;
ch.put(sub.charAt(i), k);
}else{
ch.put(sub.charAt(i), 1);
}
}
boolean flag=false;
if(ch.size()==2){
Set<Integer> values = new HashSet<Integer>(ch.values());
flag = values.size() == 1;
}
return flag;
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
int max=0;
for(int i=0;i<S.length();i++){
for(int j=S.length();j>0 && j>i;j--){
String sub=S.substring(i, j);
if(sub.length()%2==0 && sub.length()>max){
boolean b= testBalance(sub);
if(b){
//System.out.println(i+" "+j+" char count "+sub.chars().count() +" "+sub);
if(sub.length()>max){
max=sub.length();
}
}
}
}
}
// System.out.println(max);
return max;
}
private boolean testBalance(String sub) {
Map<Character,Integer> ch= new HashMap<>();
for(int i=0;i<sub.length();i++){
if(ch.containsKey(sub.charAt(i))){
Integer k=ch.get(sub.charAt(i));
k++;
ch.put(sub.charAt(i), k);
}else{
ch.put(sub.charAt(i), 1);
}
}
boolean flag=false;
if(ch.size()==2){
Set<Integer> values = new HashSet<Integer>(ch.values());
flag = values.size() == 1;
}
return flag;
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(String S) {
int max=0;
for(int i=0;i<S.length();i++){
for(int j=S.length();j>0 && j>i;j--){
String sub=S.substring(i, j);
if(sub.length()%2==0 && sub.length()>max){
boolean b= testBalance(sub);
if(b){
//System.out.println(i+" "+j+" char count "+sub.chars().count() +" "+sub);
if(sub.length()>max){
max=sub.length();
}
}
}
}
}
// System.out.println(max);
return max;
}
private boolean testBalance(String sub) {
Map<Character,Integer> ch= new HashMap<>();
for(int i=0;i<sub.length();i++){
if(ch.containsKey(sub.charAt(i))){
Integer k=ch.get(sub.charAt(i));
k++;
ch.put(sub.charAt(i), k);
}else{
ch.put(sub.charAt(i), 1);
}
}
boolean flag=false;
if(ch.size()==2){
Set<Integer> values = new HashSet<Integer>(ch.values());
flag = values.size() == 1;
}
return flag;
}
}
The following issues have been detected: timeout errors.