Advertisement
Swiftkill

EqualbyChar2

Sep 11th, 2019
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 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.                 string::const_iterator& end1,
  12.                 string::const_iterator& end2)
  13. {    
  14.     if(at2 == end2) {
  15.         if(at1 == end1)
  16.             return true; // two empty strings are equal, right?
  17.         if(*at1 == ch)
  18.             return false;    
  19.     }
  20.     else
  21.     {
  22.         if((*at1 != *at2) && (*at1 == ch || *at2 == ch)) return false;
  23.         ++at2;
  24.     }
  25.  
  26.     ++at1;    
  27.     return equalsChar( at1, at2, ch, end1, end2);
  28. }
  29.  
  30. bool equalsChar(const string& strA, const string& strB, char ch)
  31. {
  32.      // maybe do checks if strings are empty to shortcut the function?
  33.      auto it1 = strA.begin(), it2 = strB.begin();
  34.      decltype(it1) end1 = strA.end(), end2 = strB.end();
  35.    
  36.      if(strA.length() < strB.length())
  37.         std::swap(it1,it2), std::swap(end1,end2);
  38.      return equalsChar(it1, it2, ch, end1, end2 );
  39. }
  40. /*
  41. bool equalsChar(const string& strA, const string& strB, char ch) {
  42.     if (strA.size() > strB.size()) { return equalsChar(strB, strA, ch); }
  43.  
  44.     auto common = [ch](char a, char b) { return (a == ch) == (b == ch); };
  45.     return std::equal(strA.begin(), strA.end(), strB.begin(), common)
  46.         && (std::find(strB.begin() + strA.size(), strB.end(), ch) == strB.end());
  47. }*/
  48.  
  49. int main()
  50. {
  51.     std::vector<std::pair<string,string>> data= {
  52.     { "X", "X" },
  53.     { "aaaXaaaX", "abcXcbaX" },
  54.     { "XaXbXcX", "XtXoXpXdef" },
  55.     { "XaXbXcX", "XtXoXpXdXf" },
  56.     { "XXXX", "XX" },
  57.     { "aXaXbXcX", "XtXoXpX" },
  58.         {"werwer","werwerr"}};
  59.  
  60.  
  61.     for(auto& item : data)
  62.         std::cout << "equalsChar( " << item.first << ", " << item.second  << " )"
  63.               << string{ equalsChar(item.first,item.second,'X') ? " is true" : " is false"} << std::endl;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement