Given three strings A, B and C. Write a function that checks whether C is an interleaving of A and B. It may be assumed that there is no common character between A and B (Please see this for an extended solution that handles common characters also), C is said to be interleaving A and B, if it contains all characters of A and B and order of all characters in individual strings is preserved. See previous post for examples.
Pick each character of C one by one and match it with the first character in A. If it doesn’t match then match it with first character of B. If it doesn’t even match first character of B, then return false. If the character matches with first character of A, then repeat the above process from second character of C, second character of A and first character of B. If first character of C matches with the first character of B (and doesn’t match the first character of A), then repeat the above process from the second character of C, first character of A and second character of B. If all characters of C match either with a character of A or a character of B and length of C is sum of lengths of A and B, then C is an interleaving A and B.
ACBG is not interleaved of AB and CD
Time Complexity: O(m+n) where m and n are the lengths of strings A and B respectively.
Note that the above approach doesn’t work if A and B have some characters in common. For example, if string A = “AAB”, string B = “AAC” and string C = “AACAAB”, then the above method will return false. We have discussed here an extended solution that handles common characters.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print common characters of two Strings in alphabetical order
- Python code to print common characters of two Strings in alphabetical order
- Count common characters in two strings
- Number of common base strings for two strings
- Common characters in n strings
- Strings formed from given characters without any consecutive repeating characters
- Check if there is any common character in two given strings
- Check if two strings have a common substring
- Count common subsequence in two strings
- Minimum characters to be deleted from the end to make given two strings equal
- Count ways to place all the characters of two given strings alternately
- Count of same length Strings that exists lexicographically in between two given Strings
- Count the number of common divisors of the given strings
- Construct an Array of Strings having Longest Common Prefix specified by the given Array
- Pair of strings having longest common prefix of maximum length in given array
- Length of longest common prefix possible by rearranging strings in a given array
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Total length of string from given Array of strings composed using given characters
- Remove minimum number of characters so that two strings become anagram
- Find uncommon characters of the two strings