Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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.
- So, given these three strings, write a function that detects whether str3 is a valid shuffle of str1 and str2. */
- function checkString(str1, str2, str3) {
- if (str1.length + str2.length !== str3.length) return false;
- if (str3.length === 0) return true;
- const newStr3 = str3.substr(1);
- if (str1[0] === str3[0]) {
- if (str2[0] === str3[0]) {
- return checkString(str1.substr(1), str2, newStr3) || checkString(str1, str2.substr(1), newStr3);
- }
- return checkString(str1.substr(1), str2, newStr3);
- }
- if (str2[0] === str3[0]) {
- return checkString(str1, str2.substr(1), newStr3);
- }
- return false;
- }
Add Comment
Please, Sign In to add comment