LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 20

Checking rotations with (s + s).find Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 20 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <string>

bool is_rotation(const std::string& a, const std::string& b) {
    return a.size() == b.size() && (a + a).find(b) != std::string::npos;
}

int main() {
    std::cout << is_rotation("waterbottle", "erbottlewat")
              << is_rotation("abcd", "acbd")
              << is_rotation("aa", "a")
              << is_rotation("", "") << '\n';
    return 0;
}
  • A 1001 Correct
  • B 1000
  • C 1011
  • D 1101
Correct Answer: Option A

Every rotation of a appears inside a + a. "acbd" is a permutation but not a rotation; "aa" and "a" differ in length; two empty strings count as rotations of each other.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links