문자열 - 해밍 거리

박춘화·2022년 2월 24일
0

Algorithm

목록 보기
1/7

해밍 거리

동일한 길이를 가지는 두 문자열이 있을 때, 한 문자열을 다른 한 문자열로 변환하는데 필요한 최소한의 작업 횟수

const HammingDistance = (str1, str2) => {
  if (typeof str1 !== "string" || typeof str2 !== "string") {
    throw new Error("Attributes must be string!");
  }

  if (str1.length !== str2.length) {
    throw new Error("Attributes must have same length!");
  }

  let counter = 0;

  Array.from(str1).forEach((char, index) => {
    if (char !== str2[index]) {
      counter++;
    }
  });

  return counter;
};
profile
함께 성장하는 개발자

0개의 댓글