HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Codewars "Consecutive strings" kata

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
katastringscodewarsconsecutive

Problem

The challenge description is as follows:

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 "".

I solved the challenge with as a side goal to follow and use ECMAScript 6 as much as possible. All suggestions on improving the code are welcome!

Note: I did not like using k and n in code, so I used numStr and arrLen respectively.

const longestConsec = (strArr, numStr) => {
  const arrLen = strArr.length
  if (arrLen === 0 || numStr > arrLen || numStr  {
  const numConsecStr = arrLen - numStr
  const result = []
  let consecStr
  for (let i = 0; i  {
  let firstlongestString = ""
  let longestLength = 0
  for (let str of strArr) {
    strLen = str.length
    if (strLen > longestLength) {
      firstlongestString = str
      longestLength = strLen
    }
  }
  return firstlongestString
}

Solution

Some performance-related nitpicking:

  • Instead of string concatenation keep an array of string lengths;



  • Use a running window of current concatenated length: when advancing to the next string simply subtract the first window's element and add the current string length.



Thus the array will be iterated just once, no extra strings interned in the JS engine.

const longestConsec = (strings, count) => {
    let n = strings.length;
    if (!n || count  strings.length)
        return '';

    let lengths = Array(n);

    // fill the running window
    let windowLen = 0;
    for (let i = 0; i  maxLen) {
            maxLen = thisLen;
            maxLenAt = windowStart;
        }
        windowLen += stringLen - lengths[windowStart];
    }

    return strings.slice(maxLenAt, maxLenAt + count).join('');
}

Code Snippets

const longestConsec = (strings, count) => {
    let n = strings.length;
    if (!n || count <= 0 || count > strings.length)
        return '';

    let lengths = Array(n);

    // fill the running window
    let windowLen = 0;
    for (let i = 0; i < count - 1; i++) {
        let stringLen = strings[i].length;
        windowLen += stringLen;
        // fill the cache with items 0..count-1, the rest will be filled in the main loop
        lengths[i] = stringLen;
    }

    let maxLen = 0;
    let maxLenAt = 0;
    for (let i = count - 1, windowStart = 0; i < n; i++, windowStart++) {
        let stringLen = strings[i].length;
        lengths[i] = stringLen;

        let thisLen = windowLen + stringLen;
        if (thisLen > maxLen) {
            maxLen = thisLen;
            maxLenAt = windowStart;
        }
        windowLen += stringLen - lengths[windowStart];
    }

    return strings.slice(maxLenAt, maxLenAt + count).join('');
}

Context

StackExchange Code Review Q#155117, answer score: 6

Revisions (0)

No revisions yet.