John takes computer security very seriously, so he has picked a very long password to secure his files. However, although the password is very strong, it is also hard to memorize.
John decides to create a new password which is easier to remember. Therefore, it must fulfill certain requirements: he wants each character (digit or letter) to appear in the new password an even number of times (possibly zero). Also, since he is so proud of his original password, he wants the new password to be a contiguous substring of the original password.
John has trouble finding such a substring. Help him by finding, for a given string, the length of the longest substring that fulfills the requirements set out above.
Write a function:
class Solution { public int solution(String S); }
that, given a non-empty string S consisting of N characters, returns the length of the longest contiguous substring (possibly empty) of string S in which every character occurs an even number of times.
For example, given S = "6daa6ccaaa6d", the function should return 8. The longest valid password taken from S is "a6ccaaa6"; it contains four letters a, and two each of the digit 6 and letter c. Any longer substring must contain either five letters a or one letter d. Given S = "abca", the function should return 0 (note that aa is not a contiguous substring of S).
Write an efficient algorithm for the following assumptions:
- the length of string S is within the range [1..100,000];
- S consists only of lowercase letters and digits.
// 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.HashSet;
import java.util.Set;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
if(S.length() > 100000) return 0;
String maxSubStr = "";
try{
if(isAllCharsEven(S)) return S.length();
maxSubStr = "";
for(int i=0; i<S.length(); i++){
if( maxSubStr.length() > S.length()-i ) break;
for(int j=S.length(); i<j; j--){
String str = S.substring(i, j);
if( maxSubStr.length() > str.length() ) break;
if( str.length()%2 == 1 ) continue;
if( maxSubStr.length() < str.length() && isAllCharsEven(str) ){
maxSubStr = str;
}
}
}
}catch(Exception e){
//e.printStackTrace();
return 0;
}
return maxSubStr.length();
}
public boolean isAllCharsEven(String s) throws Exception{
//Set<Character> set = new HashSet<Character>();
int[] range = new int[35]; //26 + 9
for(int i=0; i<s.length(); i++){
Character ch = s.charAt(i);
if( ch >= 97 && ch <= 122 ){
range[ch-'a'+10]++;
} else if (ch >= 48 && ch <= 57) {
range[ch-'0']++;
} else
throw new Exception("Not allowed letter "+ch);
//if( set.contains(ch) ) set.remove(ch);
//else set.add(ch);
}
for(int i=0; i<range.length; i++) {
if(range[i]%2==1) return false;
}
return true;
}
}
['abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz']
// 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.HashSet;
import java.util.Set;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
if(S.length() > 100000) return 0;
String maxSubStr = "";
try{
if(isAllCharsEven(S)) return S.length();
maxSubStr = "";
for(int i=0; i<S.length(); i++){
if( maxSubStr.length() > S.length()-i ) break;
for(int j=S.length(); i<j; j--){
String str = S.substring(i, j);
if( maxSubStr.length() > str.length() ) break;
if( str.length()%2 == 1 ) continue;
if( maxSubStr.length() < str.length() && isAllCharsEven(str) ){
maxSubStr = str;
}
}
}
}catch(Exception e){
//e.printStackTrace();
return 0;
}
return maxSubStr.length();
}
public boolean isAllCharsEven(String s) throws Exception{
//Set<Character> set = new HashSet<Character>();
int[] range = new int[35]; //26 + 9
for(int i=0; i<s.length(); i++){
Character ch = s.charAt(i);
if( ch >= 97 && ch <= 122 ){
range[ch-'a'+10]++;
} else if (ch >= 48 && ch <= 57) {
range[ch-'0']++;
} else
throw new Exception("Not allowed letter "+ch);
//if( set.contains(ch) ) set.remove(ch);
//else set.add(ch);
}
for(int i=0; i<range.length; i++) {
if(range[i]%2==1) return false;
}
return true;
}
}
// 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.HashSet;
import java.util.Set;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
if(S.length() > 100000) return 0;
String maxSubStr = "";
try{
if(isAllCharsEven(S)) return S.length();
maxSubStr = "";
for(int i=0; i<S.length(); i++){
if( maxSubStr.length() > S.length()-i ) break;
for(int j=S.length(); i<j; j--){
String str = S.substring(i, j);
if( maxSubStr.length() > str.length() ) break;
if( str.length()%2 == 1 ) continue;
if( maxSubStr.length() < str.length() && isAllCharsEven(str) ){
maxSubStr = str;
}
}
}
}catch(Exception e){
//e.printStackTrace();
return 0;
}
return maxSubStr.length();
}
public boolean isAllCharsEven(String s) throws Exception{
//Set<Character> set = new HashSet<Character>();
int[] range = new int[35]; //26 + 9
for(int i=0; i<s.length(); i++){
Character ch = s.charAt(i);
if( ch >= 97 && ch <= 122 ){
range[ch-'a'+10]++;
} else if (ch >= 48 && ch <= 57) {
range[ch-'0']++;
} else
throw new Exception("Not allowed letter "+ch);
//if( set.contains(ch) ) set.remove(ch);
//else set.add(ch);
}
for(int i=0; i<range.length; i++) {
if(range[i]%2==1) return false;
}
return true;
}
}
// 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.HashSet;
import java.util.Set;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
if(S.length() > 100000) return 0;
String maxSubStr = "";
try{
if(isAllCharsEven(S)) return S.length();
maxSubStr = "";
for(int i=0; i<S.length(); i++){
if( maxSubStr.length() > S.length()-i ) break;
for(int j=S.length(); i<j; j--){
String str = S.substring(i, j);
if( maxSubStr.length() > str.length() ) break;
if( str.length()%2 == 1 ) continue;
if( maxSubStr.length() < str.length() && isAllCharsEven(str) ){
maxSubStr = str;
}
}
}
}catch(Exception e){
//e.printStackTrace();
return 0;
}
return maxSubStr.length();
}
public boolean isAllCharsEven(String s) throws Exception{
//Set<Character> set = new HashSet<Character>();
int[] range = new int[35]; //26 + 9
for(int i=0; i<s.length(); i++){
Character ch = s.charAt(i);
if( ch >= 97 && ch <= 122 ){
range[ch-'a'+10]++;
} else if (ch >= 48 && ch <= 57) {
range[ch-'0']++;
} else
throw new Exception("Not allowed letter "+ch);
//if( set.contains(ch) ) set.remove(ch);
//else set.add(ch);
}
for(int i=0; i<range.length; i++) {
if(range[i]%2==1) return false;
}
return true;
}
}
// 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.HashSet;
import java.util.Set;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
if(S.length() > 100000) return 0;
String maxSubStr = "";
try{
if(isAllCharsEven(S)) return S.length();
maxSubStr = "";
for(int i=0; i<S.length(); i++){
if( maxSubStr.length() > S.length()-i ) break;
for(int j=S.length(); i<j; j--){
String str = S.substring(i, j);
if( maxSubStr.length() > str.length() ) break;
if( str.length()%2 == 1 ) continue;
if( maxSubStr.length() < str.length() && isAllCharsEven(str) ){
maxSubStr = str;
}
}
}
}catch(Exception e){
//e.printStackTrace();
return 0;
}
return maxSubStr.length();
}
public boolean isAllCharsEven(String s) throws Exception{
//Set<Character> set = new HashSet<Character>();
int[] range = new int[35]; //26 + 9
for(int i=0; i<s.length(); i++){
Character ch = s.charAt(i);
if( ch >= 97 && ch <= 122 ){
range[ch-'a'+10]++;
} else if (ch >= 48 && ch <= 57) {
range[ch-'0']++;
} else
throw new Exception("Not allowed letter "+ch);
//if( set.contains(ch) ) set.remove(ch);
//else set.add(ch);
}
for(int i=0; i<range.length; i++) {
if(range[i]%2==1) return false;
}
return true;
}
}
// 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.HashSet;
import java.util.Set;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
if(S.length() > 100000) return 0;
String maxSubStr = "";
try{
if(isAllCharsEven(S)) return S.length();
maxSubStr = "";
for(int i=0; i<S.length(); i++){
if( maxSubStr.length() > S.length()-i ) break;
for(int j=S.length(); i<j; j--){
String str = S.substring(i, j);
if( maxSubStr.length() > str.length() ) break;
if( str.length()%2 == 1 ) continue;
if( maxSubStr.length() < str.length() && isAllCharsEven(str) ){
maxSubStr = str;
}
}
}
}catch(Exception e){
//e.printStackTrace();
return 0;
}
return maxSubStr.length();
}
public boolean isAllCharsEven(String s) throws Exception{
//Set<Character> set = new HashSet<Character>();
int[] range = new int[35]; //26 + 9
for(int i=0; i<s.length(); i++){
Character ch = s.charAt(i);
if( ch >= 97 && ch <= 122 ){
range[ch-'a'+9]++;
} else if (ch >= 48 && ch <= 57) {
range[ch-'0']++;
} else
throw new Exception("Not allowed letter "+ch);
//if( set.contains(ch) ) set.remove(ch);
//else set.add(ch);
}
for(int i=0; i<range.length; i++) {
if(range[i]%2==1) return false;
}
return true;
}
}
// 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.HashSet;
import java.util.Set;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
if(S.length() > 100000) return 0;
String maxSubStr = "";
try{
if(isAllCharsEven(S)) return S.length();
maxSubStr = "";
for(int i=0; i<S.length(); i++){
if( maxSubStr.length() > S.length()-i ) break;
for(int j=S.length(); i<j; j--){
String str = S.substring(i, j);
if( maxSubStr.length() > str.length() ) break;
if( str.length()%2 == 1 ) continue;
if( maxSubStr.length() < str.length() && isAllCharsEven(str) ){
maxSubStr = str;
}
}
}
}catch(Exception e){
//e.printStackTrace();
return 0;
}
return maxSubStr.length();
}
public boolean isAllCharsEven(String s) throws Exception{
//Set<Character> set = new HashSet<Character>();
int[] range = new int[35]; //26 + 9
for(int i=0; i<s.length(); i++){
Character ch = s.charAt(i);
if( ch >= 97 && ch <= 122 ){
range[ch-'a'+9]++;
} else if (ch >= 48 && ch <= 57) {
range[ch-'0']++;
} else
throw new Exception("Not allowed letter "+ch);
//if( set.contains(ch) ) set.remove(ch);
//else set.add(ch);
}
for(int i=0; i<range.length; i++) {
if(range[i]%2==1) return false;
}
return true;
}
}
// 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.HashSet;
import java.util.Set;
class Solution {
public int solution(String S) {
// write your code in Java SE 8
if(S.length() > 100000) return 0;
String maxSubStr = "";
try{
if(isAllCharsEven(S)) return S.length();
maxSubStr = "";
for(int i=0; i<S.length(); i++){
if( maxSubStr.length() > S.length()-i ) break;
for(int j=S.length(); i<j; j--){
String str = S.substring(i, j);
if( maxSubStr.length() > str.length() ) break;
if( str.length()%2 == 1 ) continue;
if( maxSubStr.length() < str.length() && isAllCharsEven(str) ){
maxSubStr = str;
}
}
}
}catch(Exception e){
//e.printStackTrace();
return 0;
}
return maxSubStr.length();
}
public boolean isAllCharsEven(String s) throws Exception{
//Set<Character> set = new HashSet<Character>();
int[] range = new int[35]; //26 + 9
for(int i=0; i<s.length(); i++){
Character ch = s.charAt(i);
if( ch >= 97 && ch <= 122 ){
range[ch-'a'+9]++;
} else if (ch >= 48 && ch <= 57) {
range[ch-'0']++;
} else
throw new Exception("Not allowed letter "+ch);
//if( set.contains(ch) ) set.remove(ch);
//else set.add(ch);
}
for(int i=0; i<range.length; i++) {
if(range[i]%2==1) return false;
}
return true;
}
}
The following issues have been detected: timeout errors.
random strings, all characters, N <= 500
running time: 3.08 sec., time limit: 0.10 sec.
long blocks of same letters, N <= 100,000
running time: >6.00 sec., time limit: 0.56 sec.
most characters in substring, N <= 100,000
running time: >6.00 sec., time limit: 0.66 sec.
random strings, all characters, N <= 100,000
running time: >6.00 sec., time limit: 0.69 sec.