Created
August 15, 2017 01:07
-
-
Save rpetrich/35e3e8b99b1132a68a2b808132c9ecab to your computer and use it in GitHub Desktop.
rotation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: clean all | |
CPPFLAGS = -std=c++14 | |
LINK.o = $(LINK.cc) | |
all: rotation | |
clean: | |
rm *.o rotation | |
rotation: rotation.o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string_view> | |
#include <iostream> | |
bool strings_are_rotations(std::string_view a, std::string_view b) { | |
size_t size = a.size(); | |
if (size != b.size()) | |
return false; | |
for (size_t i = 0; i < size; i++) { | |
for (size_t j = 0; ; j++) { | |
if (j == size) | |
return true; | |
if (a[j] != b[(i+j) % size]) | |
break; | |
} | |
} | |
return false; | |
} | |
void print_strings_are_rotations(std::string_view a, std::string_view b) { | |
std::cerr << a << (strings_are_rotations(a, b) ? " is a rotation of " : " is not a rotation of ") << b << std::endl; | |
} | |
int main() { | |
print_strings_are_rotations("foobar", "banana"); | |
print_strings_are_rotations("foobar", "notafoo"); | |
print_strings_are_rotations("foobar", "foobar"); | |
print_strings_are_rotations("foobar", "barfoo"); | |
print_strings_are_rotations("aaaab", "aabaa"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment