Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. public static String longestConsec(String[] strarr, int k) {
  2. if (strarr == null || strarr.length == 0 || k > strarr.length || k <= 0) {
  3. return "";
  4. }
  5.  
  6. int bestIndex = 0;
  7. int bestIndexLength = 0;
  8.  
  9. for (int i = 0; i < strarr.length - k + 1; i++) {
  10. int tmpLength = 0;
  11.  
  12. for (int arrIndex = i; arrIndex < i + k; arrIndex++) {
  13. tmpLength += strarr[arrIndex].length();
  14. }
  15.  
  16. if (tmpLength > bestIndexLength) {
  17. bestIndex = i;
  18. bestIndexLength = tmpLength;
  19. }
  20. }
  21.  
  22. StringBuilder result = new StringBuilder();
  23.  
  24. for (int index = bestIndex; index < bestIndex + k; index++) {
  25. result.append(strarr[index % strarr.length]);
  26. }
  27.  
  28. return result.toString();
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement