쉬움 0명 해결 정답률 0%

Hamming Distance

Compare two DNA strings position by position and count the differences.

배우게 될 내용

  • Compare two strings position by position
  • Count mismatches
  • Validate equal lengths

문제

번역본 보기

Compare two DNA strings position by position and count the differences. Only characters at the same position are compared; do not insert, delete, or shift characters. No biology knowledge is needed.

Implement hamming_distance(first, second).

Input

first: The first DNA string.

second: The equal-length second DNA string.

Output

Return the number of positions with different characters.

제약 조건

Both strings have the same length, from 0 to 100,000. Characters are uppercase A, C, G, or T.

예시

예시 1
입력:
first = "ACCT"
second = "AGCA"

예상 출력:
expected = 2
입력:
std::string first = "ACCT";
std::string second = "AGCA";

예상 출력:
long long expected = 2;
입력:
string first = "ACCT";
string second = "AGCA";

예상 출력:
long expected = 2;

설명

The second position is C versus G and the fourth is T versus A. The first and third match: two differences.

예시 2
입력:
first = "GATT"
second = "GATT"

예상 출력:
expected = 0
입력:
std::string first = "GATT";
std::string second = "GATT";

예상 출력:
long long expected = 0;
입력:
string first = "GATT";
string second = "GATT";

예상 출력:
long expected = 0;

설명

All four positions match, so no differences are counted.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Compare both strings one position at a time and count when the characters differ.

힌트 2

A single pass with paired iteration is enough; no rearranging or substring searches are needed.

힌트 3

Remember that equal-length inputs can still be empty, which should yield zero differences.

예상 복잡도

O(n) time, O(1) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.