Skip to content

Commit 037805a

Browse files
committed
Implement solution for "A. Spell Check" problem
1 parent 443203b commit 037805a

1 file changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Problem: A. Spell Check
3+
4+
Problem Statement:
5+
Timur allows any permutation of the letters of his name "Timur" as a valid spelling,
6+
but the spelling must contain exactly one uppercase 'T' and the remaining letters
7+
('i', 'm', 'u', 'r') must be lowercase. Given a string s, check if it is a valid spelling
8+
of Timur's name.
9+
10+
Approach:
11+
1. If the length of the string is not 5, it cannot be "Timur".
12+
2. Sort the given string.
13+
3. Sort the reference string "Timur".
14+
4. If both sorted strings are equal, then s is a valid permutation of "Timur".
15+
16+
Time Complexity:
17+
O(n log n), where n = 5 (effectively constant).
18+
19+
Space Complexity:
20+
O(1), uses only constant extra space.
21+
22+
Example:
23+
Input:
24+
1
25+
5
26+
miurT
27+
28+
Output:
29+
YES
30+
31+
Question Link:
32+
https://codeforces.com/problemset/problem/1722/A
33+
34+
Submission Link:
35+
https://codeforces.com/contest/1722/submission/345095137
36+
*/
37+
38+
#include <iostream>
39+
#include <algorithm>
40+
using namespace std;
41+
42+
void spellCheck() {
43+
int n;
44+
cin >> n;
45+
46+
string s;
47+
cin >> s;
48+
49+
if (n != 5) {
50+
cout << "NO\n";
51+
return;
52+
}
53+
54+
string name = "Timur";
55+
56+
sort(s.begin(), s.end());
57+
sort(name.begin(), name.end());
58+
59+
if (s == name) {
60+
cout << "YES\n";
61+
} else {
62+
cout << "NO\n";
63+
}
64+
}
65+
66+
int main() {
67+
int t;
68+
cin >> t;
69+
70+
while (t--) {
71+
spellCheck();
72+
}
73+
74+
return 0;
75+
}

0 commit comments

Comments
 (0)