Generalized Abbreviation in C++

Suppose there is a word. We have to define a function that can generate the generalized abbreviations of a word.
So, if the input is like "word", then the output will be ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
To solve this, we will follow these steps −
Define an array ret
Define an array ret
Define a function solve(), this will take s, idx,
Define a function solve(), this will take s, idx,
if idx >= size of s, then −insert s at the end of retreturn
if idx >= size of s, then −
insert s at the end of ret
insert s at the end of ret
return
return
y := substring of s from index 0 to idx - 1
y := substring of s from index 0 to idx - 1
i := size of y
i := size of y
num := blank string
num := blank string
while (i >= 0 and y[i] <= ASCII of '9' and y[i] >= ASCII of '0'), do −num := y[i] + num(decrease i by 1)
while (i >= 0 and y[i] <= ASCII of '9' and y[i] >= ASCII of '0'), do −
num := y[i] + num
num := y[i] + num
(decrease i by 1)
(decrease i by 1)
if i is not equal to size of y, then −ret := substring of s from index (0 to idx - (size of y - 1 - i) - 1) concatenate (number + 1)as string concatenate substring of s from index (0 to idx)s1 := num + 1 as strings2 := num as stringif size of s1 is same as size of s2, then −Otherwisesolve(ret, idx + 1)
if i is not equal to size of y, then −
ret := substring of s from index (0 to idx - (size of y - 1 - i) - 1) concatenate (number + 1)as string concatenate substring of s from index (0 to idx)
ret := substring of s from index (0 to idx - (size of y - 1 - i) - 1) concatenate (number + 1)as string concatenate substring of s from index (0 to idx)
s1 := num + 1 as string
s1 := num + 1 as string
s2 := num as string
s2 := num as string
if size of s1 is same as size of s2, then −
if size of s1 is same as size of s2, then −
Otherwisesolve(ret, idx + 1)
Otherwise
solve(ret, idx + 1)
solve(ret, idx + 1)
Otherwiseprev := s[idx]s[idx] := '1'solve(s, idx + 1)s[idx] := prev
Otherwise
prev := s[idx]
prev := s[idx]
s[idx] := '1'
s[idx] := '1'
solve(s, idx + 1)
solve(s, idx + 1)
s[idx] := prev
s[idx] := prev
solve(s, idx + 1)
solve(s, idx + 1)
From the main method do the following −
From the main method do the following −
solve(word, 0)
solve(word, 0)
return ret
return ret
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;
}
class Solution {
public:
   vector<string> ret;
   void solve(string s, int idx){
      if (idx >= s.size()) {
         ret.push_back(s);
         return;
      }
      string y = s.substr(0, idx);
      int i = y.size() - 1;
      string num = "";
      while (i >= 0 && y[i] <= '9' && y[i] >= '0') {
         num = y[i] + num;
         i--;
      }
      if (i != y.size() - 1) {
         string ret = s.substr(0, idx - (y.size() - 1 - i)) + to_string(stoi(num) + 1) + s.substr(idx + 1);
         string s1 = to_string(stoi(num) + 1);
         string s2 = to_string(stoi(num));
         if (s1.size() == s2.size())
            solve(ret, idx);
         else
            solve(ret, idx + 1);
      }
      else {
         char prev = s[idx];
         s[idx] = '1';
         solve(s, idx + 1);
         s[idx] = prev;
      }
      solve(s, idx + 1);
   }
   vector<string< generateAbbreviations(string word){
      solve(word, 0);
      return ret;
   }
};
main(){
   Solution ob;
   print_vector(ob.generateAbbreviations("hello"));
}
hello
[5, 4o, 3l1, 3lo, 2l2, 2l1o, 2ll1, 2llo, 1e3, 1e2o, 1e1l1, 1e1lo, 1el2, 1el1o, 1ell1, 1ello, h4, h3o, h2l1, h2lo, h1l2, h1l1o, h1ll1, h1llo, he3, he2o, he1l1, he1lo, hel2, hel1o, hell1, hello, ]