I have written the following code for finding the minimum deletion distance between two strings
enter code here #include <iostream> #include <string> using namespace std; int DeletionDistance(const string& str1, const string& str2, int len1, int len2){ int index1=0; int index2=0; int count=0; int str1len=str1.length(); int str2len=str2.length(); //calculate the base case if a string is empty, the deletion distance is the length of the other string //since we need to delete all the other chars from the non empty string in order to match the two strings if(str1len<=0) return str2len; else if(str2len<=0) return str1len; else{ //the strings are non empty. Recursively calculate the min del distance if(str1[index1]==str2[index2]){ //the chars match , hence the deletion distance would depend on the remaining chars in both strings return DeletionDistance(str1.substr(index1+1,len1), str2.substr(index2+1,len2), len1, len2); } else //the chars dont match return (1+min(DeletionDistance(str1.substr(index1+1,len1), str2.substr(index2,len2), len1, len2), DeletionDistance(str1.substr(index1,len1), str2.substr(index2+1,len2), len1, len2))); } } int deletionDistance( const string& str1, const string& str2 ) { int len1 = str1.length(); int len2 = str2.length(); return DeletionDistance(str1, str2, len1, len2); } In this code , where we recursively calculate the minimum deletion distance between two strings how does one calculate the Time and Space complexity? I was told that the time and space complexity for this solution is O(ab) where, a = len of string 1 b = len of string 2 I would really appreciate some explanation or pointers on how to start calculating Big O for recursive solutions like this. I was able to calculate bigO for simpler recursive solutions like Fibonacci series , factorial etc , but this beats me.
len1andlen2parameters seem completely pointless.O(2^min(a,b)), but it would beO(ab)if you used some kind of memoization (which you are not here). It would be nice to better to hear from an algorithm master than from me, but I believe this is the case.