Consecutive strings

题目

You are given an array strarr of strings and an integer k. Your task is to return the first longest string consisting of k consecutive strings taken in the array.

Example:
longest_consec([“zone”, “abigail”, “theta”, “form”, “libe”, “zas”, “theta”, “abigail”], 2) –> “abigailtheta”

n being the length of the string array, if n = 0 or k > n or k <= 0 return “”.

Note
consecutive strings : follow one after another without an interruption

思路

这道题目就是输出最长的K个连续字符串。

答案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function longestConsec(strarr, k) {
let n = strarr.length;
let str_f = ""
if (n === 0 || k <= 0 || n < k) {
return "";
} else {
for (let i = 0; i < n - k + 1; i++) {
let str_t = "";
for (let j = 0; j < k; j++) {
str_t += strarr[i + j];
}
if (str_f.length < str_t.length) {
str_f = str_t;
}
}
return str_f;
}
}