문제
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.
예시
입력:
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.
입력:
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에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.