Advertisement
ekanshlohiya98

validShuffle(61)

May 26th, 2022
539
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Check if a string is a valid shuffle of two other strings
  2. //s1=xy s2=12
  3. //res=x1y2  valid      res=x2y1  invalid
  4.  
  5. bool validShuffle(string &s1 ,string &s2, string &res)
  6. {
  7.     if(s1.size()+s2.size()!=res.size()) return false;
  8.     int i=0,j=0,k=0;
  9.     while(k<res.size())
  10.     {
  11.         if(i<s1.size() && s1[i]==res[k]) i++;
  12.         else if(j<s2.size() && s2[j]==res[k]) j++;
  13.         else return false;
  14.  
  15.         k++;
  16.     }
  17.     return true;
  18. }
Advertisement
RAW Paste Data Copied
Advertisement