Check if a string is a rotation of another

Given two strings, check if string 2 is a rotation of string1.

string1: hello

string2: olhel

returns: true

  1. Check if strings are of equal length
  2. Combine string1 with string1
  3. See if string 2 is within string1
bool isRotation(string str1, string str2) {
    if(str1.length() != str2.length()) {
        return false;
    }

    str1.append(str1);
    if(str1.find(str2) != std::string::npos) {
        return true;
    }
    return false;
}

results matching ""

    No results matching ""