leetcode interleaving string

leetcode interleaving string

Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.

For example,

Given:
s1 = “aabcc”,
s2 = “dbbca”,

When s3 = “aadbbcbcac”, return true.
When s3 = “aadbbbaccc”, return false.

leetcode interleaving string

 

The solution involves dynamic programming approach.

C++ code:

{!{code}!}czoxMDkyOlwiDQo8ZW0+Ym9vbCBpc0ludGVybGVhdmUoc3RyaW5nIHMxLCBzdHJpbmcgczIsIHN0cmluZyBzMykgezwvZW0+DQo8ZW17WyYqJl19PiBpbnQgTSA9IHMxLnNpemUoKSwgTiA9IHMyLnNpemUoKSwgSyA9IHMzLnNpemUoKTs8L2VtPg0KPGVtPiBpZiAoTSArIE4gIT0gS3tbJiomXX0pIHJldHVybiBmYWxzZTs8L2VtPg0KDQo8ZW0+Ly9ib29sIGRwW04rMV1bTSsxXTs8L2VtPg0KDQo8ZW0+Ym9vbCAqKiBkcCA9IChie1smKiZdfW9vbCoqKW1hbGxvYyhOKzEpOzwvZW0+DQo8ZW0+IGZvcihpbnQgaT0wIDsgaSAmbHQ7TiArMSA7IGkrKyk8L2VtPg0KPGVtPiB7PC97WyYqJl19ZW0+DQo8ZW0+IGRwW2ldPShib29sICopbWFsbG9jKE0rMSk7PC9lbT4NCjxlbT4gfTwvZW0+DQo8ZW0+IGRwWzBdWzBdID0gdHJ1ZXtbJiomXX07PC9lbT4NCjxlbT4gZm9yIChpbnQgaSA9IDE7IGkgJmx0Oz0gTjsgKytpKTwvZW0+DQo8ZW0+IGRwW2ldWzBdID0gZHBbaS0xXVswe1smKiZdfV0gJmFtcDsmYW1wOyBzMltpLTFdID09IHMzW2ktMV07PC9lbT4NCjxlbT4gZm9yIChpbnQgaiA9IDE7IGogJmx0Oz0gTTsgKytqKTx7WyYqJl19L2VtPg0KPGVtPiBkcFswXVtqXSA9IGRwWzBdW2otMV0gJmFtcDsmYW1wOyBzMVtqLTFdID09IHMzW2otMV07PC9lbT4NCg0KPGVtPntbJiomXX1mb3IgKGludCBpID0gMTsgaSAmbHQ7PSBOOyArK2kpPC9lbT4NCjxlbT4gZm9yIChpbnQgaiA9IDE7IGogJmx0Oz0gTTsgKytqKTwve1smKiZdfWVtPg0KPGVtPiB7PC9lbT4NCjxlbT4gZHBbaV1bal0gPSBkcFtpLTFdW2pdICZhbXA7JmFtcDsgczJbaS0xXSA9PSBzM1tpK2otMV17WyYqJl19IHx8PC9lbT4NCjxlbT4gZHBbaV1bai0xXSAmYW1wOyZhbXA7IHMxW2otMV0gPT0gczNbaStqLTFdOzwvZW0+DQo8ZW0+IGNvdXQgJntbJiomXX1sdDsmbHQ7IGRwW2ldW2pdOzwvZW0+DQo8ZW0+IH08L2VtPg0KPGVtPiBmb3IgKGludCBpID0gMTsgaSAmbHQ7PSBOOyArK2kpPC9le1smKiZdfW0+DQo8ZW0+IHs8L2VtPg0KPGVtPiBmb3IgKGludCBqID0gMTsgaiAmbHQ7PSBNOyArK2opPC9lbT4NCjxlbT4gY291dCAmbHQ7Jmx7WyYqJl19dDsgZHBbaV1bal0gKyBcIiBcIiA7PC9lbT4NCjxlbT4gY291dCAmbHQ7Jmx0OyBlbmRsOzwvZW0+DQo8ZW0+IH08L2VtPg0KPGVtPiBye1smKiZdfWV0dXJuIGRwW05dW01dOzwvZW0+DQo8ZW0+fTwvZW0+DQoNClwiO3tbJiomXX0={!{/code}!}

The complexity of the program is O(MN) where M and N is length of strings s1 and s2 respectively.

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at writeulearn@gmail.com along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.