Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String longestConsec(String[] strarr, int k) {
- if (strarr == null || strarr.length == 0 || k > strarr.length || k <= 0) {
- return "";
- }
- int bestIndex = 0;
- int bestIndexLength = 0;
- for (int i = 0; i < strarr.length - k + 1; i++) {
- int tmpLength = 0;
- for (int arrIndex = i; arrIndex < i + k; arrIndex++) {
- tmpLength += strarr[arrIndex].length();
- }
- if (tmpLength > bestIndexLength) {
- bestIndex = i;
- bestIndexLength = tmpLength;
- }
- }
- StringBuilder result = new StringBuilder();
- for (int index = bestIndex; index < bestIndex + k; index++) {
- result.append(strarr[index % strarr.length]);
- }
- return result.toString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement