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) {
// write your code in Java SE 8
for(int i = S.length(); i)
}
}
// 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
}
}
}
// 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
for(int j = ){
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
for(int j = 0; j < ){
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
for(int j = 0; j <= S.length() - i; j++){
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
for(int j = 0; j <= S.length() - i; j++){
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
for(int j = 0; j <= S.length() - i; j++){
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx)
for(int j = 0; j <= S.length() - i; j++){
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']
idx++;
}
for(int j = 0; j <= S.length() - i; j++){
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int j = 0; j <= S.length() - i; j++){
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int j = 1; j <= S.length() - i; j++){
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int j = 1; j <= S.length() - i; j++){
counter[]
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(i) - 'a']
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(i-1) - 'a']--;
counter[S.charAt(i-1) - 'a']--;
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt() - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
if(isTrue)
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int k = 0; k < )
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int k = 0; k < 26; k++){
int
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int k = 0; k < 26; k++){
int distinct = 0;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
int distinct = 0;
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int k = 0; k < 26; k++){
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
int distinct = 0;
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
distinct = 0
for(int k = 0; k < 26; k++){
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
int distinct = 0;
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
distinct = 0
for(int k = 0; k < 26; k++){
if(distinct <= 2)
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
int distinct = 0;
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
distinct = 0
for(int k = 0; k < 26; k++){
if(distinct <= 2){
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
int distinct = 0;
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
distinct = 0
for(int k = 0; k < 26; k++){
if(distinct <= 2){
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
int distinct = 0;
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
distinct = 0
for(int k = 0; k < 26; k++){
if(distinct <= 2){
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
distinct = 0
for(int k = 0; k < 26; k++){
if(distinct <= 2){
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
distinct = 0
for(int k = 0; k < 26; k++){
if(set.size() <= 2){
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
distinct = 0
for(int k = 0; k < 26; k++){
if(set.size() <= 2){
set.add(k);
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
distinct = 0
for(int k = 0; k < 26; k++){
if(set.size() <= 2){
if()
set.add(k);
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
distinct = 0
for(int k = 0; k < 26; k++){
if(set.size() <= 2){
if(counter[k] > 0)
set.add(k);
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
for(int k = 0; k < 26; k++){
if(set.size() <= 2){
if(counter[k] > 0)
set.add(k);
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(set.size() <= 2){
if(counter[k] > 0)
set.add(k);
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0)
set.add(k);
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0)
set.add(k);
}
if(set.size() <= 2){
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0)
set.add(k);
}
if(set.size() == 2){
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
if(set.size() == 2){
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
List<Integer> list = new ArrayList();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
List<Integer> list = new ArrayList();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
list = new ArrayList();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
List<Integer> list = new ArrayList();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
list = new ArrayList();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<>
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it =
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
while(it.hasNext()){
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
while(it.hasNext()){
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
List<Integer> list = new ArrayList();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
list = new ArrayList();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
while(it.hasNext()){
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
List<Integer> list = new ArrayList();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
list = new ArrayList();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
while(it.hasNext()){
list.add()
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
while(it.hasNext()){
list.add()
}
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
int first =
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first])
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
}
}
return 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) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
}
}
return 0;
}
}
Solution.java:10: error: cannot find symbol Set<Integer> set = new HashSet(); ^ symbol: class Set location: class Solution Solution.java:10: error: cannot find symbol Set<Integer> set = new HashSet(); ^ symbol: class HashSet location: class Solution Solution.java:19: error: cannot find symbol set = new HashSet(); ^ symbol: class HashSet location: class Solution Solution.java:26: error: cannot find symbol Iterator<Integer> it = set.iterator(); ^ symbol: class Iterator location: class Solution Solution.java:35: error: cannot find symbol set = new HashSet(); ^ symbol: class HashSet location: class Solution Solution.java:42: error: cannot find symbol Iterator<Integer> it = set.iterator(); ^ symbol: class Iterator location: class Solution 6 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");
import java.util.*
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
}
}
return 0;
}
}
Solution.java:7: error: ';' expected import java.util.* ^ 1 error
// 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");
import java.util.*;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
}
}
return 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");
import java.util.*;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(i) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
}
}
return 0;
}
}
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7 at java.lang.String.charAt(String.java:658) at Solution.solution(Solution.java:18) at Exec.run(exec.java:46) at Exec.main(exec.java:31)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7 at java.lang.String.charAt(String.java:658) at Solution.solution(Solution.java:18) at Exec.run(exec.java:46) at Exec.main(exec.java:31)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7 at java.lang.String.charAt(String.java:658) at Solution.solution(Solution.java:18) at Exec.run(exec.java:46) at Exec.main(exec.java:31)
// 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");
import java.util.*;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(idx) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
}
}
return 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");
import java.util.*;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(idx) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
}
}
return 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");
import java.util.*;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(idx) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
}
}
return 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");
import java.util.*;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
Set<Integer> set = new HashSet();
for(int i = S.length(); i >= 2; i--){
int[] counter = new int[26];
int idx = 0;
while(idx < i){
counter[S.charAt(idx) - 'a']++;
idx++;
}
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
for(int j = 1; j <= S.length() - i; j++){
counter[S.charAt(j-1) - 'a']--;
counter[S.charAt(i-1+j) - 'a']++;
set = new HashSet();
for(int k = 0; k < 26; k++){
if(counter[k] > 0){
set.add(k);
}
}
if(set.size() == 2){
Iterator<Integer> it = set.iterator();
int first = it.next();
int second = it.next();
if(counter[first]==counter[second])
return counter[first]*2;
}
}
}
return 0;
}
}
The following issues have been detected: timeout errors.