Thursday, 28 July 2011

The "FREEDOM" Question



// given a word say "FREEDOM" and another srch string "DOMFREE" find whether the search string can be wrapped to form the first string.

#include <iostream>
#include <String>
using namespace std;

bool isWrappable(string s1, string s2)
{
if(s1.length() != s2.length())
return false;

int index = s2.find(s1[1]);

if(index == -1)
return false;


while(index != -1)
{

string subst1 = s2.substr(index-1);
string subst2 = s2.substr(0,index-1);

string strconsol = subst1 + subst2;

if(s1 == strconsol)
return true;

index = s2.find(s1[1],index+1);
}
return false;
}

int main()
{
string str1("FFREF");
string str2("REFFF");

if(isWrappable(str1,str2))
cout << "Can Wrap";
else
  cout << "Cant wrap";

getchar();
return 0;

}

No comments:

Post a Comment