Official

A - Hamming Distance Editorial by en_translator


If you are new to learning programming and do not know where to start, please try Problem A “Welcome to AtCoder” from practice contest. There you can find a sample code for each language.
Also, if you are not familiar with problems in programming contests, we recommend you to try some problems in “AtCoder Beginners Selection” (https://atcoder.jp/contests/abs).


You are given an integer \(N\) and two length-\(N\) strings \(S\) and \(T\), and asked to find the number of indices \(i\ (1\leq i\leq N)\) with \(S_i\neq T_i\). (Here, \(S_i\) denotes the \(i\)-th character of \(S\).)

To write a code that solves this problem, we need the following three building blocks:

  • Input and output of integers and strings
  • Loop structure to inspect each of \(i=1,2,\dots,N\)
  • Conditional branch to check if \(S_i\neq T_i\).

Their representation varies depending on your programming language. Please also refer to tutorials of your language.

See also the following sample code (C++ and Python).

Sample code (C++) :

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    cin >> n;
    string s, t;
    cin >> s >> t;
    int ans = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] != t[i]) {
            ++ans;
        }
    }
    cout << ans << endl;
}

Sample code (Python) :

n = int(input())
s = input()
t = input()
ans = 0
for i in range(n):
  if s[i] != t[i]:
    ans += 1
print(ans)

posted:
last update: