Advertisement
Swiftkill

Equalbychar

Sep 11th, 2019
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. // This file is a "Hello, world!" in C++ language by GCC for wandbox.
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. using std::string;
  8. bool equalsChar(string::const_iterator& at1,
  9.                 string::const_iterator& at2,
  10.                 const char& ch,
  11.                 const string::const_iterator& end1,
  12.                 const string::const_iterator& end2)
  13. {
  14.     if((at1 == end1) && (at2 == end2))
  15.         return true; // two empty strings are equal, right?
  16.     if(at1 != end1)
  17.     {
  18.        if(at2 != end2)
  19.        {
  20.             if((*at1 != *at2) && (*at1 == ch || *at2 == ch))
  21.                 return false;
  22.             return equalsChar( ++at1, ++at2, ch, end1, end2);
  23.        }
  24.        else
  25.        {
  26.             if(*at1 == ch)
  27.                 return false;
  28.             return equalsChar( ++at1, at2, ch, end1, end2);
  29.        }
  30.     }
  31.     else {
  32.         if(*at2 == ch ) return false;
  33.         return equalsChar( ++at2, at1, ch,end2, end1);
  34.     }
  35. }
  36.  
  37. bool equalsChar(const string& strA, const string& strB, char ch)
  38. {
  39.      // maybe do checks if strings are empty to shortcut the function?
  40.      auto it1 = strA.begin(), it2 = strB.begin();
  41.      return equalsChar(it1, it2, ch, strA.end(), strB.end() );
  42. }
  43. /*
  44. bool equalsChar(const string& strA, const string& strB, char ch) {
  45.     if (strA.size() > strB.size()) { return equalsChar(strB, strA, ch); }
  46.  
  47.     auto common = [ch](char a, char b) { return (a == ch) == (b == ch); };
  48.     return std::equal(strA.begin(), strA.end(), strB.begin(), common)
  49.         && (std::find(strB.begin() + strA.size(), strB.end(), ch) == strB.end());
  50. }*/
  51.  
  52. int main()
  53. {
  54.     std::vector<std::pair<string,string>> data= {
  55.     { "X", "X" },
  56.     { "aaaXaaaX", "abcXcbaX" },
  57.     { "XaXbXcX", "XtXoXpXdef" },
  58.     { "XaXbXcX", "XtXoXpXdXf" },
  59.     { "XXXX", "XX" },
  60.     { "aXaXbXcX", "XtXoXpX" },
  61.         {"werwerwe","erwer"}};
  62.  
  63.  
  64.     for(auto& item : data)
  65.         std::cout << "equalsChar( " << item.first << ", " << item.second  << " )"
  66.               << string{ equalsChar(item.first,item.second,'X') ? " is true" : " is false"} << std::endl;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement