समस्या
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
दोनों strings को एक-एक position पर compare करो और जहाँ characters अलग हों, वहाँ count बढ़ाओ।
संकेत 2
एक single pass और paired iteration काफी है; rearranging या substring searches की जरूरत नहीं है।
संकेत 3
याद रखो कि equal-length inputs खाली भी हो सकते हैं, और तब zero differences मिलेंगे।
अपेक्षित जटिलता
समय O(n), अतिरिक्त स्थान O(1)
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।