Friday, July 2, 2021

Published July 02, 2021 by Anonymous with 1 comment

Lexicographically largest possible by merging two strings by adding one character at a time

Lexicographically largest possible by merging two strings by adding one character at a time

Geek-O-Lympics

Given two strings S and T, the task is to merge these two strings by adding one character at a time from the beginning of either string to form a resultant string. The resultant string should be lexicographically the largest string that can be formed by merging the strings S and T

Examples:

Input: S = “dbcbb”, T = “cdbbb”
Output : dcdbcbbbbb

Input : S = geeks“, T = forgeeks”
Output : gforgeekseeks

Approach: The simplest idea to solve the problem is to greedily select the first characters from the string which is lexicographically bigger than others. Therefore, Greedy algorithm and Recursion can be used to solve the problem. Follow the steps below to solve the problem:


  •  If either of the string lengths is 0, then return S + T as the answer.
  • If S is lexicographically larger than T, then return S[0] + largestMerge(S.substr(1), T).
  • Otherwise, take the first character of T and call the recursive function largestMerge(S, T.substr(1)).

Below is the implementation of the above approach:

C++

  

#include <bits/stdc++.h>

using namespace std;

  

string largestMerge(string s1, string s2)

{

    

    

    if (s1.size() == 0 || s2.size() == 0)

        return s1 + s2;

  

    

    

    if (s1 > s2) {

  

        

        

        return s1[0] + largestMerge(s1.substr(1), s2);

    }

  

    

    

    

    return s2[0] + largestMerge(s1, s2.substr(1));

}

  

int main()

{

    

    string s1 = "geeks";

    string s2 = "forgeeks";

  

    

    cout << largestMerge(s1, s2) << endl;

  

    return 0;

}

Output
gforgeekseeks

Time Complexity: O(M×N), where M and N are the length of string s1 and s2 respectively.
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


Adblock test (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

1 comment:

  1. Lexicographically Largest Possible By Merging Two Strings By Adding One Character At A Time ~ Cnc Software >>>>> Download Now

    >>>>> Download Full

    Lexicographically Largest Possible By Merging Two Strings By Adding One Character At A Time ~ Cnc Software >>>>> Download LINK

    >>>>> Download Now

    Lexicographically Largest Possible By Merging Two Strings By Adding One Character At A Time ~ Cnc Software >>>>> Download Full

    >>>>> Download LINK D0

    ReplyDelete