https://atcoder.jp/contests/abc302/tasks/abc302_c
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define ull unsigned long long
#define vb v.begin()
#define ve v.end()
#define vc v.clear()
#define vs (int)v.size()
#define ss (int)s.size()
#define rs(i) resize(i)
#define ft first
#define sd second
#define inf 0x3f3f3f3f
#define P pair<int, int>
#define Pll pair<ll, ll>
#define Pis pair<int, string>
#define Psi pair<string, int>
#define Pic pair<int, char>
#define Pci pair<char, int>
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fore(i, a, b) for (int i = a; i <= b; i++)
#define rofn(i, a, b) for (int i = a; i > b; i--)
#define rofe(i, a, b) for (int i = a; i >= b; i--)
#define Ios ios::sync_with_stdio(false), cin.tie(0)
const ll N = 2010;
const ll M = 100010;
const int INF = 2147483647;
using namespace std;
signed main()
{
//Ios;
int n, m;
cin >> n >> m;
vector<string> s(n);
fore(i, 1, n)
{
cin >> s[i-1];
}
sort(s.begin(), s.end());
do
{
bool ok = true;
for (int i = 0; i < n - 1; i++) {
int cnt = 0;
for (int j = 0; j < m; j++) {
if (s[i][j] != s[i + 1][j]) cnt += 1;
}
if (cnt != 1) ok = false;
}
if (ok) {
cout << "Yes" << endl;
return 0;
}
} while (next_permutation(s.begin(),s.end()));
cout << "No" << endl;
return 0;
}
/*
1
10
10 2 5 6 1 9 3 8 4 7
*/
/*******************************************
坤坤助我
............................................
............................................
............................................
............................................
.............................1ffLf1.........
............................GGGGGGGC........
....................8CCCftCCCCC1;LLG,.......
..................:CC8CCC.CCCCCCii..........
.................CC88CCCC.CCCCCCC...........
................CC88CCCCC .8.:8CCC..........
................888 CCCCC.88888G;;,.........
,................888CC88.888888888,.........
:::...............CCC888.888888888..........
:::::............Gi1tC8f888888C88,..........
:::::::,........111tttfffffLG8888...........
:::::::::,....ii111ttttttfffttG.............
::::::::::::i111tttffffffffffttt............
::::::::::11tttttfff....fffffftt ...........
..:::::::1Lf1tff,..........fffff1...........
....,:::iiftG::::::.........,LLGtL..........
::::::;:i11t:;;;;;;::,,,,,,,,, f1ii,::,,,,,,
::::::::i11t:::::::::::::::::::f111:::::::::
::::::::111:::::::::::::::::::::t111::::::::
:::::::i1if::::::,,,,,,,,,,,,,,:i111::::::,:
,,,,,::,8G,,,,,,,,,,,,,,,,,,,,,,,tL1:,,,,,,,
,,,,,,:LGL;,,,,,,,,,,,,,,,,,,,,,,,CLf,,,,,,,
,,,,,;LGGG,,,,:,,,,,,,,,,,,,,,,,,,LGCG,,,,,,
*******************************************/