簡単 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つずつ開いてみましょう。

ヒント 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用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。