×

ENIGMA03 - Editorial

Cut and Paste

Author and Editorialist : Vishal Sharma

Easy-Medium

DP

EXPLANATION:

Brute force approach

Time complexity - O( |str1| * |str2| )
Result – time limit exceeded

First, check whether all characters in str2 are also present in str1. If not, it is impossible to obtain str2 using str1. Then, write a nested loop to search each character of str2 in str1.

Dynamic programming solution

time complexity – O( |s1|*26 + |s2|)

First create a matrix l[|s1+1|][256]
l[i][j]=first occurrence of character j in the string s1[i..|s1|-1]
if j is not present in the s1[i...|s1-1|], l[i][j]=|s1|

if (s1[i] == j) l[i][j] = i;

else if (i + 1 < n1) l[i][j] = l[i + 1][j];

SOLUTION

This question is marked "community wiki".

464
accept rate: 10%

 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,680
×2,170
×1,672
×42

question asked: 16 Oct '15, 19:36

question was seen: 610 times

last updated: 16 Oct '15, 22:59