#K50372. Exactly One Character Difference Pair
Exactly One Character Difference Pair
Exactly One Character Difference Pair
You are given N strings of equal length M. Your task is to determine whether there exists a pair of strings that differ by exactly one character. In other words, check if there is any two distinct strings such that when compared character by character, exactly one position has a different character.
Note: All strings have exactly M characters and the comparisons are case-sensitive.
Input/Output: Read the input from standard input and write the result to the standard output.
If such a pair exists, output YES
, otherwise output NO
.
Mathematical representation:
Let the strings be denoted by \( s_1, s_2, \ldots, s_N \) where each \( s_i \) is of length \( M \). We need exist some \( i, j \) with \( 1 \le i < j \le N \) such that:
\[ \sum_{k=1}^{M} \mathbb{1}_{\{s_i[k] \neq s_j[k]\}} = 1 \]inputFormat
The first line contains two integers N
and M
separated by a space, where N
is the number of strings and M
is the length of each string.
This is followed by N
lines, each line containing one string of length M
.
You should read input from standard input (stdin).
outputFormat
Output a single line: YES
if there exists a pair of strings that differ by exactly one character, or NO
otherwise. The result should be written to standard output (stdout).
4 5
apple
apply
ample
align
YES