-2

I am new to this platform so please bear with me. My program was based on the longest substring problem with no mismatches using Dynamic Programming. Here is a link to the D-P solution.

https://www.geeksforgeeks.org/longest-common-substring-dp-29/.

Here is a link to the original problem.

https://www.hackerrank.com/challenges/substring-diff/problem

I basically used the table in the d-p solution to store the number of mismatches in the two substrings rather than using it to calculate the number of matches as it was used in the original problem. for all values in the table less than or equal to I stored the maximum possible length and printed it.

Though my solution works fine with small test cases it gives wrong answers in the larger ones.

Here is my code in c++.

int substringDiff(int k, string s1, string s2) {
vector <vector<int>> v(s1.size()+1,vector<int>(s2.size()+1,0));
int len=0;
for(int i=1;i<=s1.size();i++) 
{
for(int j=1;j<=s2.size();j++)
{
    if(s1[i-1]==s2[j-1])
        v[i][j]=v[i-1][j-1];
    else
        v[i][j]=v[i-1][j-1]+1;
    if(v[i][j]<=k)
        len=max(len,min(i,j));
 }
}

return len;
}

I searched on the web and found some better solutions but I don't understand why my code doesn't work. please help.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • 1
    "doesn't work" is not a sufficiently technical description of your problem. Can you explain what it does that is undesirable, or what it doesn't do that you need it to? – tadman Aug 26 '20 at 22:15
  • Tip: When writing tricky code use a unit test framework like [Catch2](https://github.com/catchorg/Catch2) to verify your function is working correctly under a variety of test circumstances. This is a lot easier to repeat than manual testing and builds confidence in your solutions. – tadman Aug 26 '20 at 22:16
  • ⟼This code could benefit greatly by adopting an [indentation style](https://en.wikipedia.org/wiki/Indentation_style) and applying it consistently. Indentation conveys structure and intent which makes it easier for us to understand your code without having to invest a lot of time deciphering it, and it can also make mistakes more obvious as they stand out visually. – tadman Aug 26 '20 at 22:17
  • TIp: Use `const std::string&` when accepting string arguments or you'll end up making a lot of pointless copies of the string objects involved. – tadman Aug 26 '20 at 22:17
  • @tadman by doesn't work I mean it's giving me answers less than the expected answer only for larger inputs. – Adeeb Akhtar Aug 26 '20 at 22:19
  • 1
    You're going to need to show us how you used this code so we can reproduce the problem. "Larger" is not code we can use. Sorry for being stubborn here but it really help if you shows us *exactly what you did* and *exactly what you got*. – tadman Aug 26 '20 at 22:19
  • 2 tabriz torino – Adeeb Akhtar Aug 26 '20 at 22:24
  • Can you include some C++ code for that, and include the output? Edit your question to include new code. Helps is reproduce the *exact* issue you have. – tadman Aug 26 '20 at 22:25
  • Your solution don't even work with some small case: `substringDiff(0, "xa", "ya")` returned `0` while `1` is expected. – MikeCAT Aug 26 '20 at 22:27
  • 2 tabriz torino. here I am getting 4 as my answer which is correct and I am passing similar test cases quite easily. But when it comes to cases like in which the string input size is larger than a standard line I am getting answers less than correct answers. which might point towards faulty algorithm. i need help in finding that. if you want the test case i can send it to you separately. – Adeeb Akhtar Aug 26 '20 at 22:31
  • 1
    Give [mre] a read to get a better feel for what is expected from a code sample on this site. – user4581301 Aug 26 '20 at 22:33
  • 1
    Can you explain why you _believe_ this algorithm should work? Generally part of designing an algorithm is _proving_ that it does what what you want for all inputs. If you can state your reasoning for how you believe it works we might be able to point out if you've made a mistaken assumption, an error in reasoning or an implementation bug. But without that it's pretty hard to help you. – Weeble Aug 26 '20 at 22:40

1 Answers1

2

Your program don't give correct answer when both of the longest common substrings are not prefixes of each input strings.

For example, substringDiff(0, "xa", "ya") returns 0 while 1 is expected.

This is because only number of differences from beginning of either strings are calculated.

Example table for substringDiff(0, "xa", "ya"):

  y a
x 1 1
a 1 1

To obtain correct answer, using this table,

  1. Set a length to check if satisfying common substrings of the length exists.
  2. Do checking using the length: it can be done by brute-force using the table (v[i][j] - v[i-length][j-length] is the number of mismatch of substrings that ends with i-th character of s1 and j-th character of s2)
  3. Using this check, do binary search for maximum length that satisfying common substring exists.
MikeCAT
  • 73,922
  • 11
  • 45
  • 70