taesik

Consecutive strings 본문

functions and trick/typescript_javascript

Consecutive strings

taesikk 2022. 1. 29. 12:07

You are given an array(list) 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.

 

 

export function longestConsec(strarr: string[], k: number): string {
  if (strarr.length === 0 || k > strarr.length || k <= 0) {
    return "";
  } 
  
  return strarr
    .reduce((defender, _, idx, target) => {
      const challenger = target.slice(idx, idx + k).join("");
      
      return challenger.length > defender.length ? challenger : defender;
    }, "");
}

'functions and trick > typescript_javascript' 카테고리의 다른 글

Counting Duplicates  (0) 2022.01.29
Backspaces in string  (0) 2022.01.29
digitRoot  (0) 2022.01.29
Coordinates Validator  (0) 2022.01.28
Count Friday 13th given year  (0) 2022.01.28