Skip to content

Instantly share code, notes, and snippets.

@ravichandrae
Last active March 16, 2016 06:14
Show Gist options
  • Save ravichandrae/b33cf2777fc9530ada8a to your computer and use it in GitHub Desktop.
Save ravichandrae/b33cf2777fc9530ada8a to your computer and use it in GitHub Desktop.
How to check if a given string is a double string possible by deleting a character.
#include <iostream>
#include <vector>
#include <climits>
#include <cstdlib>
using namespace std;
bool is_double(string &input) {
size_t len = input.size();
if( len % 2 == 1 )
return false;
int i;
int mid = len/2;
for( i = 0; i < mid; i++ ) {
if( input[i] != input[i+mid] )
return false;
}
return true;
}
bool is_sub_seq(string &longer, string &shorter) {
int i, j;
bool mismatch = false;
for( i = 0, j = 0; i < longer.size() && j < shorter.size(); ) {
if( longer[i] != shorter[j] ) {
if( mismatch ) {
return false;//more than one mismatch; not a double string
}
else {
mismatch = true; //one mismatch is fine
i++; //skip this character from the longer half
}
}
else
{
i++;
j++;
}
}
return true;
}
bool can_be_double(string &input) {
size_t len = input.size();
if( len < 2 )
return false;
if( len % 2 == 0 )
return is_double(input);
else
{
string longer = input.substr(0, len/2+1);
string shorter = input.substr(len/2+1, len/2);
if( is_sub_seq(longer, shorter) )
return true;
shorter = input.substr(0, len/2);
longer = input.substr(len/2, len/2+1);
return is_sub_seq(longer,shorter);
}
}
int main() {
cin.sync_with_stdio(false);
int t;
cin >> t;
while(t--) {
string input;
cin >> input;
if( can_be_double(input) )
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment