top of page

Daily Coding Problem #1

Writer's picture: Mischievous_FaceMischievous_Face

Updated: Feb 19, 2019

// Given two strings A and B, return whether or not A can be shifted some number of times to get B.

// For example, if A is abcde and B is cdeab, return true.If A is abc and B is acb, return false.


#include <iostream>

#include <map>

#include <vector>

#include <string>


using namespace std;


bool canSwap(string str1, string str2)

{

vector<char> charVec = vector<char>();

size_t found;


if (str1.length() == str2.length())

{

for (unsigned i = 0; i < str1.length(); ++i)

{

found = str2.find(str1[i]);

if (found != string::npos)

{

str2.erase(str2.begin() + found);

continue;

}

else

return false;

}

}

else

return false;


return true;

}



int main(int argc, char** argv)

{

string a = "god is good";

string b = "good is god";


bool result = canSwap(a, b);


if (result)

cout << "We got it!" << endl;

else

cout << "Nope, can't swap" << endl;


return 0;

}

29 views0 comments

Recent Posts

See All

Comments


© 2020 Josh Painter

bottom of page