Asked 5 months ago
0Comments
0 Views
Given a string print YES if the string repeats after half by neglecting the case. For example,
Input Format
A string (may even contain spaces)
Constraints
It may contain any valid characters (not just alphabets)
Output Format
YES or NO.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string input;
getline(cin, input);
// Convert to lowercase
transform(input.begin(), input.end(), input.begin(), ::tolower);
// Remove middle character if odd length
if (input.length() % 2 != 0) {
input.erase(input.length() / 2, 1);
}
// Split the string
int halfLength = input.length() / 2;
string firstHalf = input.substr(0, halfLength);
string secondHalf = input.substr(halfLength);
// Compare and output result
if (firstHalf == secondHalf) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
This solution follows these steps:
This approach handles all the given cases:
Share