// Determine if a string has all-unique characters. #define CATCH_CONFIG_MAIN #include #include #include #include using string_view = std::experimental::string_view; auto count(string_view str) { std::unordered_map c; for (auto ch : str) { // default value is zero. c[ch] += 1; } return c; } bool is_permutation(string_view lhs, string_view rhs) { // unordered_map does elementwise comparison. return count(lhs) == count(rhs); } TEST_CASE("cc12", "is_permutation") { REQUIRE(is_permutation("abc", "abc") == true); REQUIRE(is_permutation("abc", "cba") == true); REQUIRE(is_permutation("abc", "def") == false); REQUIRE(is_permutation("aabbbc", "abacbb") == true); REQUIRE(is_permutation("aabbbc", "abacb") == false); }