Lexicographically Smallest Equivalent String in C++


Suppose we have strings A and B of the same length, now we can say A[i] and B[i] are equivalent characters. So for example, if A = "abc" and B = "cde", then we have 'a' = 'c', 'b' = 'd' and 'c' = 'e'. The equivalent characters follow the usual rules of any equivalence relation:

  • Reflexivity: 'a' = 'a'

  • Symmetry: 'a' = 'b' implies 'b' = 'a'

  • Transitivity: 'a' = 'b' and 'b' = 'c' implies 'a' = 'c'

Now for example, given the equivalency information from A and B above, S = "eed", "acd", and "aab" are equivalent strings, and "aab" is the lexicographically smallest equivalent string of S. Here we have to find the lexicographically smallest equivalent string of S by using the equivalency information from A and B. Return all words that can be formed in this manner, in lexicographical order.

So if the input is like A = “parker”, B = “morris” and S = “parser”, then the output will be “makkek”. This is because based on the equivalency information in A and B, we can group their characters as [m,p], [a,o], [k,r,s], [e,i]. So the characters in each group are equivalent and sorted in lexicographical order. So the answer is "makkek".

To solve this, we will follow these steps −

  • Create an array called parent

  • Define a method called getParent(), this will take character x

  • if parent[x – ASCII of ‘a’] is -1, then return x – ASCII of ‘a’

  • parent[x – ASCII of ’a’] := getParent(ASCII of ‘a’ + parent[x – ASCII of ‘a’])

  • return parent[x – ASCII of ’a’]

  • Create another method called union(), this takes a and b

  • parentA := getParent(a), and parent := getParent(b)

  • so if parentA = parent, then return otherwise when parentA < parent, then parent[parentB] := parentA, and otherwise parent[parentA] := parent

  • From the main method, do the following −

  • set parent := an array of size 26 and fill this using -1

  • for i in range 0 to 25

    • perform union(A[i], B[i])

  • create one blank string ret

  • for i in range 0 to size of S

    • ret := ret + getParent(S[i]) + ‘a’

  • return ret

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
   public:
   vector <int> parent;
   int getParent(char x){
      //cout << x << "-- " << x-'a' << endl;
      if(parent[x - 'a'] == -1) return x - 'a';
      return parent[x - 'a'] = getParent('a' + parent[x - 'a']);
   }
   void unionn(char a, char b){
      int parentA = getParent(a);
      int parentB = getParent(b);
      if(parentA == parentB) return;
      if(parentA < parentB){
         parent[parentB] = parentA;
      }else{
         parent[parentA] = parentB;
      }
   }
   string smallestEquivalentString(string A, string B, string S) {
      parent = vector <int>(26, -1);
      for(int i = 0; i < A.size(); i++){
         unionn(A[i], B[i]);
      }
      string ret = "";
      for(int i = 0; i < S.size(); i++){
         ret += getParent(S[i]) + 'a';
      }
      return ret;
   }
};
main(){
   Solution ob;
   cout <<
   (ob.smallestEquivalentString("parker","morris","parser"));
}

Input

"parker"
"morris"
"parser"

Output

makkek

Updated on: 30-Apr-2020

619 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements