Program to get operations to convert one string to another in C++

C++Server Side ProgrammingProgramming

Suppose we have two strings S and T. We have to find the shortest sequence of operations that changes S to T. Here the operations are basically either deleting or inserting a character.

So, if the input is like S = "xxxy" T = "xxyy", then the output will be ["x", "x", "-x", "y", "+y"], this means place first two x's, then remove 3rd x, then place y then add a new y.

To solve this, we will follow these steps −

  • make a table dp of size 505 x 505
  • Define a function help(), this will take i, j, S, T,
  • if i is same as size of S and j is same as size of T, then −
    • return dp[i, j] = 0
  • if i is same as size of S, then −
    • return dp[i, j] = 1 + help(i, j + 1, S, T)
  • if j is same as size of T, then −
    • return dp[i, j] = 1 + help(i + 1, j, S, T)
  • if dp[i, j] is not equal to -1, then −
    • return dp[i, j]
  • dontDo := 1e5, del := 0, insert := 0
  • if S[i] is same as T[j], then −
    • dontDo := help(i + 1, j + 1, S, T)
  • del := 1 + help(i + 1, j, S, T)
  • insert := 1 + help(i, j + 1, S, T)
  • minVal := min({dontDo, del, insert})
  • return dp[i, j] = minVal
  • Define a function getPath(), this will take i, j, S, T, curr, an array ret,
  • if curr is same as 0 and i is same as size of S and j is same as size of T, then −
    • return
  • if i < size of S and j < size of T and S[i] is same as T[j] and dp[i + 1, j + 1] is same as curr, then −
    • insert string(1, S[i]) at the end of ret
    • getPath(i + 1, j + 1, S, T, curr, ret)
  • otherwise when dp[i + 1, j] + 1 is same as curr, then −
    • insert ("-" concatenate string(1, S[i])) at the end of ret
    • getPath(i + 1, j, S, T, curr - 1, ret)
  • Otherwise
    • insert ("+" concatenate string(1, T[j])) at the end of ret
    • getPath(i, j + 1, S, T, curr - 1, ret)
  • From the main method do the following −
  • fill the dp with -1
  • Define an array ret
  • x := help(0, 0, S, T)
  • getPath(0, 0, S, T, x, ret)
  • return ret

Example (C++)

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v) {
   cout << "[";
   for (int i = 0; i < v.size(); i++) {
      cout << v[i] << ", ";
   }
   cout << "]" << endl;
}
int dp[505][505];
class Solution {
   public:
   int help(int i, int j, string& S, string& T) {
      if (i == S.size() && j == T.size())
         return dp[i][j] = 0;
      if (i == S.size())
         return dp[i][j] = 1 + help(i, j + 1, S, T);
      if (j == T.size())
         return dp[i][j] = 1 + help(i + 1, j, S, T);
      if (dp[i][j] != -1)
         return dp[i][j];
      int dontDo = 1e5;
      int del = 0;
      int insert = 0;
      if (S[i] == T[j])
         dontDo = help(i + 1, j + 1, S, T);
      del = 1 + help(i + 1, j, S, T);
      insert = 1 + help(i, j + 1, S, T);
      int minVal = min({dontDo, del, insert});
      return dp[i][j] = minVal;
   }
   void getPath(int i, int j, string& S, string& T, int curr, vector<string>& ret) {
      if (curr == 0 && i == S.size() && j == T.size())
         return;
      if (i < S.size() && j < T.size() && S[i] == T[j] && dp[i + 1][j + 1] == curr) {
         ret.push_back(string(1, S[i]));
         getPath(i + 1, j + 1, S, T, curr, ret);
      }else if (dp[i + 1][j] + 1 == curr) {
         ret.push_back("-" + string(1, S[i]));
         getPath(i + 1, j, S, T, curr - 1, ret);
      }else {
         ret.push_back("+" + string(1, T[j]));
         getPath(i, j + 1, S, T, curr - 1, ret);
      }
   }  
   vector<string> solve(string S, string T) {
      memset(dp, -1, sizeof dp);
      vector<string> ret;
      int x = help(0, 0, S, T);
      getPath(0, 0, S, T, x, ret);
      return ret;
   }
};
vector<string> solve(string source, string target) {
   return (new Solution())->solve(source, target);
}
main(){
   string S = "xxxy", T = "xxyy";
   print_vector(solve(S, T));
}

Input

"xxxy", "xxyy"

Output

[x, x, -x, y, +y, ]
raja
Published on 12-Dec-2020 09:22:27
Advertisements