Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This file is a "Hello, world!" in C++ language by GCC for wandbox.
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using std::string;
- bool equalsChar(string::const_iterator& at1,
- string::const_iterator& at2,
- const char& ch,
- const string::const_iterator& end1,
- const string::const_iterator& end2)
- {
- if((at1 == end1) && (at2 == end2))
- return true; // two empty strings are equal, right?
- if(at1 != end1)
- {
- if(at2 != end2)
- {
- if((*at1 != *at2) && (*at1 == ch || *at2 == ch))
- return false;
- return equalsChar( ++at1, ++at2, ch, end1, end2);
- }
- else
- {
- if(*at1 == ch)
- return false;
- return equalsChar( ++at1, at2, ch, end1, end2);
- }
- }
- else {
- if(*at2 == ch ) return false;
- return equalsChar( ++at2, at1, ch,end2, end1);
- }
- }
- bool equalsChar(const string& strA, const string& strB, char ch)
- {
- // maybe do checks if strings are empty to shortcut the function?
- auto it1 = strA.begin(), it2 = strB.begin();
- return equalsChar(it1, it2, ch, strA.end(), strB.end() );
- }
- /*
- bool equalsChar(const string& strA, const string& strB, char ch) {
- if (strA.size() > strB.size()) { return equalsChar(strB, strA, ch); }
- auto common = [ch](char a, char b) { return (a == ch) == (b == ch); };
- return std::equal(strA.begin(), strA.end(), strB.begin(), common)
- && (std::find(strB.begin() + strA.size(), strB.end(), ch) == strB.end());
- }*/
- int main()
- {
- std::vector<std::pair<string,string>> data= {
- { "X", "X" },
- { "aaaXaaaX", "abcXcbaX" },
- { "XaXbXcX", "XtXoXpXdef" },
- { "XaXbXcX", "XtXoXpXdXf" },
- { "XXXX", "XX" },
- { "aXaXbXcX", "XtXoXpX" },
- {"werwerwe","erwer"}};
- for(auto& item : data)
- std::cout << "equalsChar( " << item.first << ", " << item.second << " )"
- << string{ equalsChar(item.first,item.second,'X') ? " is true" : " is false"} << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement