코드로 우주평화

Maximum Length Difference 본문

나는 이렇게 학습한다/Algorithm & SQL

Maximum Length Difference

daco2020 2022. 3. 11. 16:40
반응형

Description:

You are given two arrays a1 and a2 of strings. Each string is composed with letters from a to z. Let x be any string in the first array and y be any string in the second array.

Find max(abs(length(x) − length(y)))

If a1 and/or a2 are empty return -1 in each language except in Haskell (F#) where you will return Nothing (None).

Example:

a1 = ["hoqq", "bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"]
a2 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"]
mxdiflg(a1, a2) --> 13

Bash note:

  • input : 2 strings with substrings separated by ,
  • output: number as a string

 

 

Solution:

1. If the array 'a1' or 'a2' is empty, '-1' is returned.
2. Subtract the strings in each array and store the result in the array.
3. Returns the largest number in the array.

 

 

function mxdiflg(a1, a2) {
  if (a1 == false | a2 == false) {
    return -1
  }
  let arr = []
  for (i of a1) {
    for (j of a2) {
      arr.push(Math.abs(i.length-j.length))
    }
  }
  return Math.max(...arr)
}

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

A square of squares  (0) 2022.03.13
Count the divisors of a number  (0) 2022.03.12
Testing 1-2-3  (0) 2022.03.10
Find the middle element  (0) 2022.03.09
Highest and Lowest  (0) 2022.03.08