# ¿Por qué se requiere programación dinámica para esto?

The problem statement can be found aquí. It says there that the DP solution will take O(m*n) time. But why do we need DP in there? I've written the following solution and not sure if DP is required at all.

``````    String str1,str2;
str1 = "OldSite:GeeksforGeeks.org";
str2 = "NewSite:GeeksQuiz.com";

int i,j,k;
int count,currentMax =0;
for(i = 0; i < str1.length();i++){
count = 0;
k = i;
for(j = 0; j < str2.length() && k < str1.length();j++){
if(str1.charAt(k) == str2.charAt(j)){
count++;
k++;
}
else if(count > currentMax)
currentMax = count;
continue;
}
if(count > currentMax)
currentMax = count;
}

System.out.println(currentMax);
``````

Even my solution takes O(m*n) but I don't seem to be doing it the DP way.

What I've done is that check for each character in string1, what is the max length available in string 2.

Basically I think this is the brute force method.

Anything wrong with my solution?

EDITAR As Mentioned in the comments section.

making changes to produce correct result changing the inner for loop as

``````for(j = 0; j < str2.length() && k < str1.length();j++){
if(str1.charAt(k) == str2.charAt(j)){
count++;
k++;
}
else if(count > currentMax){
currentMax = count;
count = 0;
k=i;
}
else{
count = 0;
k=i;
}
continue;
}
``````

``````for(j = 0; j < str2.length() && k < str1.length();j++){
if(str1.charAt(k) == str2.charAt(j)){
count++;
k++;
}
else if(count > currentMax){
currentMax = count;
count = 0;
k=i;
if(str1.charAt(k) == str2.charAt(j)){
count++;
k++;
}
}
else{
count = 0;
k=i;
if(str1.charAt(k) == str2.charAt(j)){
count++;
k++;
}
}
continue;
}
``````

preguntado el 12 de junio de 14 a las 11:06

The link you post says the problem can be solved using a suffix tree, so DP isn't needed. -

## 1 Respuestas

I think you code for longest common substring is not working try input :-

``````str1 = "swring";
str2 = "skwkring";
``````

The Longest common substring here is "ring" which is 4 characters but your code gives "6" answer. Here i think your code is just counting the common characters in the strings which is "swring" so result is 6.

You cannot solve this using brute force in En M) though it seems obvious so DP is used.

Editar: -

Your edited solutions covers more problems but still is not perfect for centrally positioned strings due to same reasons that it is codicioso diferente a DP

check :-

``````str1 = "sbsringada";
str2 = "cdssringasd";
``````

The correct ans is `6` que sea `"sringa"` but the code outputs `5`. The reason being it gets codicioso at "s" before `"sringa"` in str2.

Editar:-

This will go on for ever where you will give a code i will give you where it doesnt work unless you dont find the actual brute algorithm or prove that your code is correct mathematically.

As you have not proved it , i will give you the actual brute force code which i had once written for the same problem . It is very simple and intuitive to understand but very inefficient.

Pseudo code :-

``````int max = 0;

for(int i=0;i<str1.length,i++) {

for(int j=0;j<str2.length;j++) {
int k = 0;
for(;j+k<str2.length && i+k < str1.length ;k++) {
if(str1[i+k]!=str[j+k])
break;
}

if(k>max)
max = k;
}

}

print(max);
``````

The time complexity this code is `O(m*n*min(m,n))`

For further progress you need to prove that your code does similar to this one like in case of DP its proven by induction.

Respondido el 12 de junio de 14 a las 15:06

But you can actually see intuitively that O(mn) using the above method is possible (even though I might've screwed up some assignments). If I take one character at a time from string 1, and see what is the max length of substring in string 2, and I take the maximum for all the characters in String 1. Now, complexity to check the max substring for a character in string 1 would be size of string 2. Hence overall for m characters in string one, it will be msize of string 2. - Kraken

I think you are confusing the problem with en.wikipedia.org/wiki/Longest_common_subsequence_problem. - Jorge Nechifor

@GeoAoe not i am not see my example - vikram bhat

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.