#P7376. Consecutive Column Permutation for Identical Rows

    ID: 20573 Type: Default 1000ms 256MiB

Consecutive Column Permutation for Identical Rows

Consecutive Column Permutation for Identical Rows

You are given an \(N \times N\) matrix consisting of lowercase letters and an integer \(K\). Determine whether there exists a contiguous block of \(K\) columns such that by arbitrarily reordering (i.e. permuting) the letters within each row in that block only, the matrix can have two rows that become completely identical.

Details:

  • Only the letters in the chosen contiguous block of columns may be rearranged within the same row. The letters in the remaining columns must remain in their original positions.
  • For two rows to be identical after the allowed reordering: they must have the same characters in the positions outside the chosen block, and the multiset of characters in the chosen block must be identical between the two rows (since any permutation is allowed within the block).
  • Mathematically, if you choose a block of columns from \(i\) to \(i+K-1\) (using 1-indexed columns) for each row \(r\), denote:

[ \text{Fixed}(r) = \text{row}[1..i-1] \Vert \text{row}[i+K..N], \quad \text{Block}(r) = { \text{row}[i], \text{row}[i+1], \dots, \text{row}[i+K-1] }. ]

Then, the answer is "Yes" if there exists a block \(i\) such that for some pair of distinct rows \(r_1\) and \(r_2\):

[ \text{Fixed}(r_1) = \text{Fixed}(r_2) \quad \text{and} \quad \text{sorted}(\text{Block}(r_1)) = \text{sorted}(\text{Block}(r_2)). ]

Otherwise, output "No".

inputFormat

The first line contains two integers \(N\) and \(K\) (where \(1 \leq K \leq N\)) separated by a space.

Each of the following \(N\) lines contains a string of length \(N\) representing a row of the matrix.

outputFormat

Output a single line: "Yes" if such a block exists, or "No" otherwise.

sample

3 2
abc
acc
abb
No

</p>