Guest User

Untitled

a guest
Dec 17th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. /* Given three strings, str1, str2, str3. str3 is said to be a shuffle of str1 and str2 if it can be formed by interleaving the characters of str1 and str2 in such a manner that it maintains left to right ordering from each string. Given str1 = 'abc' and str2 = 'def', str3 = 'dabecf' is a valid shuffle since it preserves the character ordering of the two strings.
  2.  
  3. So, given these three strings, write a function that detects whether str3 is a valid shuffle of str1 and str2. */
  4.  
  5. function checkString(str1, str2, str3) {
  6. if (str1.length + str2.length !== str3.length) return false;
  7. if (str3.length === 0) return true;
  8. const newStr3 = str3.substr(1);
  9. if (str1[0] === str3[0]) {
  10. if (str2[0] === str3[0]) {
  11. return checkString(str1.substr(1), str2, newStr3) || checkString(str1, str2.substr(1), newStr3);
  12. }
  13. return checkString(str1.substr(1), str2, newStr3);
  14. }
  15. if (str2[0] === str3[0]) {
  16. return checkString(str1, str2.substr(1), newStr3);
  17. }
  18. return false;
  19. }
Add Comment
Please, Sign In to add comment