An array of N words is given. Each word consists of small letters ('a' − 'z'). Our goal is to concatenate the words in such a way as to obtain a single word with the longest possible substring composed of one particular letter. Find the length of such a substring.
Write a function:
class Solution { public int solution(String[] words); }
that, given an array words containing N strings, returns the length of the longest substring of a word created as described above.
Examples:
1. Given N=3 and words=["aabb", "aaaa", "bbab"], your function should return 6. One of the best concatenations is words[1] + words[0] + words[2] = "aaaaaabbbbab". The longest substring is composed of letter 'a' and its length is 6.
2. Given N=3 and words=["xxbxx", "xbx", "x"], your function should return 4. One of the best concatenations is words[0] + words[2] + words[1] = "xxbxxxxbx". The longest substring is composed of letter 'x' and its length is 4.
3. Given N=4 and words=["dd", "bb", "cc", "dd"], your function should return 4. One of the best concatenations is words[0] + words[3] + words[1] + words[2] = "ddddbbcc". The longest substring is composed of letter 'd' and its length is 4.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- all the words are non−empty and consist only of lowercase letters (a−z);
- S denotes the sum of the lengths of words; S is an integer within the range [1..100,000].
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_1 =
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.){
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(!len_head_index_1.keySet().contains(first_c)){
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 =
len_head_index_1.get(first_c) >
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(){
}
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c,)
}
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
}
len_head_index_2.put(first_c, current_1);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
} else if (){
}
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}
} else {
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(){
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put();
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
/*
if(i==0&&j==1){
System.out.println(head_len[j]);
}
*/
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
int current_num_1 =
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_index_1.put(first_c, head_num);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_index_2.put(first_c, current_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, head_num);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_index_1.put(first_c,head_num);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_2 = len_head_index_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, );
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.put(first_c,i);){
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(first_c)){
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(first_c)){
int j_idx = len_head_index_1.get(first_c);
if(i==j_idx){
if(){
}
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(first_c)){
int j_idx = len_head_index_1.get(first_c);
if(i==j_idx){
if(){
}
continue;
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(first_c)){
int j_idx = len_head_index_1.get(first_c);
if(i==j_idx){
if(len_head_index_2.keySet().contains(first_c)){
}
continue;
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(first_c)){
int j_idx = len_head_index_1.get(first_c);
if(i==j_idx){
if(len_head_index_2.keySet().contains(first_c)){
j_idx = len_head_index_2.get(first_c);
} else {
continue;
}
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(first_c)){
int j_idx = len_head_index_1.get(first_c);
if(i==j_idx){
if(len_head_index_2.keySet().contains(first_c)){
j_idx = len_head_index_2.get(first_c);
} else {
continue;
}
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(first_c)){
int j_idx = len_head_index_1.get(first_c);
if(i==j_idx){
if(len_head_index_2.keySet().contains(first_c)){
j_idx = len_head_index_2.get(first_c);
} else {
continue;
}
}
int len_concat = head_len[j]+tail_len[i];
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(first_c)){
int j_idx = len_head_index_1.get(first_c);
if(i==j_idx){
if(len_head_index_2.keySet().contains(first_c)){
j_idx = len_head_index_2.get(first_c);
} else {
continue;
}
}
int len_concat = head_len[j_idx]+tail_len[i];
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(first_c)){
int j_idx = len_head_index_1.get(first_c);
if(i==j_idx){
if(len_head_index_2.keySet().contains(first_c)){
j_idx = len_head_index_2.get(first_c);
} else {
continue;
}
}
int len_concat = head_len[j_idx]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(first_c)){
int j_idx = len_head_index_1.get(first_c);
if(i==j_idx){
if(len_head_index_2.keySet().contains(first_c)){
j_idx = len_head_index_2.get(first_c);
} else {
continue;
}
}
int len_concat = head_len[j_idx]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
Solution.java:112: error: cannot find symbol if(len_head_index_1.keySet().contains(first_c)){ ^ symbol: variable first_c location: class Solution Solution.java:113: error: cannot find symbol int j_idx = len_head_index_1.get(first_c); ^ symbol: variable first_c location: class Solution Solution.java:115: error: cannot find symbol if(len_head_index_2.keySet().contains(first_c)){ ^ symbol: variable first_c location: class Solution Solution.java:116: error: cannot find symbol j_idx = len_head_index_2.get(first_c); ^ symbol: variable first_c 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(tail)){
int j_idx = len_head_index_1.get(first_c);
if(i==j_idx){
if(len_head_index_2.keySet().contains(first_c)){
j_idx = len_head_index_2.get(first_c);
} else {
continue;
}
}
int len_concat = head_len[j_idx]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(tail)){
int j_idx = len_head_index_1.get(tail);
if(i==j_idx){
if(len_head_index_2.keySet().contains(tail)){
j_idx = len_head_index_2.get(tail);
} else {
continue;
}
}
int len_concat = head_len[j_idx]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(tail)){
int j_idx = len_head_index_1.get(tail);
if(i==j_idx){
if(len_head_index_2.keySet().contains(tail)){
j_idx = len_head_index_2.get(tail);
} else {
continue;
}
}
int len_concat = head_len[j_idx]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
['aabbaa', 'aaba', 'aba', 'aba', 'aba']
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else {
if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}else{
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(tail)){
int j_idx = len_head_index_1.get(tail);
if(i==j_idx){
if(len_head_index_2.keySet().contains(tail)){
j_idx = len_head_index_2.get(tail);
} else {
continue;
}
}
int len_concat = head_len[j_idx]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else {
if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}else{
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(tail)){
int j_idx = len_head_index_1.get(tail);
if(i==j_idx){
if(len_head_index_2.keySet().contains(tail)){
j_idx = len_head_index_2.get(tail);
} else {
continue;
}
}
int len_concat = head_len[j_idx]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
/*
List<Integer> head_list = head_index.get(tail);
if(head_list == null){
continue;
}
for(Integer j : head_list){
if(i==j){
continue;
}
int len_concat = head_len[j]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
*/
}
return max_num;
}
}
['aabbaa', 'aaba', 'aba', 'aba', 'aba']
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else {
if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}else{
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(tail)){
int j_idx = len_head_index_1.get(tail);
if(i==j_idx){
if(len_head_index_2.keySet().contains(tail)){
j_idx = len_head_index_2.get(tail);
} else {
continue;
}
}
int len_concat = head_len[j_idx]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else {
if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}else{
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(tail)){
int j_idx = len_head_index_1.get(tail);
if(i==j_idx){
if(len_head_index_2.keySet().contains(tail)){
j_idx = len_head_index_2.get(tail);
} else {
continue;
}
}
int len_concat = head_len[j_idx]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
['aabbaa', 'aaba', 'aba', 'aba', 'aba']
// 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[] words) {
// write your code in Java SE 8
int N = words.length;
//Map<Character, List<Integer>> head_index = new HashMap<>();
Map<Character, Integer> len_head_num_1 = new HashMap<>();
Map<Character, Integer> len_head_num_2 = new HashMap<>();
Map<Character, Integer> len_head_index_1 = new HashMap<>();
Map<Character, Integer> len_head_index_2 = new HashMap<>();
int max_num = 0;
Map<Character, Integer> consective_num = new HashMap<>();
int[] tail_len = new int[N];
int[] head_len = new int[N];
List<Integer> no_consective_list = new ArrayList<>();
for(int i=0;i<N;i++){
String word = words[i];
if(word.length()==0){
continue;
}
int num_consective = 0;
int head_num = 0;
boolean has_change = false;
for(int j=0;j<word.length();j++){
if(j>0 && word.charAt(j)!=word.charAt(j-1)){
num_consective=0;
has_change = true;
}
num_consective++;
if(!has_change){
head_num++;
}
if(num_consective > max_num){
max_num = num_consective;
}
}
char first_c = word.charAt(0);
if(!has_change){
if(!consective_num.keySet().contains(first_c)){
consective_num.put(first_c,0);
}
consective_num.put(first_c,consective_num.get(first_c)+word.length());
} else {
/*
if(!head_index.keySet().contains(first_c)){
head_index.put(first_c, new ArrayList<Integer>());
}
head_index.get(first_c).add(i);
*/
if(len_head_index_1.keySet().contains(first_c)){
int current_index_1 = len_head_index_1.get(first_c);
int current_num_1 = len_head_num_1.get(first_c);
if(head_num > current_num_1){
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
len_head_num_2.put(first_c, current_num_1);
len_head_index_2.put(first_c, current_index_1);
}else {
if(len_head_index_2.keySet().contains(first_c)){
int current_index_2 = len_head_index_2.get(first_c);
int current_num_2 = len_head_num_2.get(first_c);
if(head_num > current_num_2){
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}else{
len_head_index_2.put(first_c, i);
len_head_num_2.put(first_c, head_num);
}
}
} else {
len_head_num_1.put(first_c, head_num);
len_head_index_1.put(first_c,i);
}
no_consective_list.add(i);
head_len[i]=head_num;
tail_len[i]=num_consective;
}
}
Iterator<Character> it = consective_num.keySet().iterator();
while(it.hasNext()){
char c = it.next();
if(consective_num.get(c) > max_num){
max_num = consective_num.get(c);
}
}
for(Integer i: no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
char head = words[i].charAt(0);
if(consective_num.keySet().contains(head)){
int len = head_len[i] + consective_num.get(head);
if(len > max_num){
max_num = len;
}
}
if(consective_num.keySet().contains(tail)){
int len = tail_len[i] + consective_num.get(tail);
if(len > max_num){
max_num = len;
}
}
}
for(Integer i : no_consective_list){
char tail = words[i].charAt(words[i].length()-1);
if(len_head_index_1.keySet().contains(tail)){
int j_idx = len_head_index_1.get(tail);
if(i==j_idx){
if(len_head_index_2.keySet().contains(tail)){
j_idx = len_head_index_2.get(tail);
} else {
continue;
}
}
int len_concat = head_len[j_idx]+tail_len[i];
if(consective_num.keySet().contains(tail)){
len_concat += consective_num.get(tail);
}
if(len_concat > max_num){
max_num = len_concat;
}
}
}
return max_num;
}
}
The solution obtained perfect score.
S - sum of the lengths of words
N = 4, S = 30, each word contains long substring with one letter.
N = 6, S = 50, the longest prefix and the longest suffix occur in the same word.
N = 25, S = 400, each word contains long substring with one letter.
N = 25, S = 400, the longest prefix and the longest suffix occur in the same word.
S = 20,000, each word contains long substring with one letter.
S = 20,000, the longest prefix and the longest suffix occur in the same word.
S = 100,000, each word contains long substring with one letter.
S = 100,000, the longest prefix and the longest suffix occur in the same word.