Short Encoding of Words in C++

C++Server Side ProgrammingProgramming

Suppose we have a list of words, we may encode it by writing a reference string S and a list of indexes A. So for example, let us consider if the list of words is ["time", "me", "bell"], then we can write it as S = "time#bell#" and indexes = [0, 2, 5]. Here for each index, we will recover the word by reading from the reference string from that index until we reach the "#" symbol.

So we have to find what is the length of the shortest reference string S possible that encodes the given words? So for the given example, the output will be 10.

To solve this, we will follow these steps −

  • Define insertNode method, this will take head and string s
  • curr := head, flag := false.
  • for i in range size of s – 1 down to 0
    • x := s[i]
    • if m[x] of curr is null, then set flat := true and create a new node, into m[x] of curr.
    • curr := m[x] of curr
  • return size of s when flag is true, otherwise 0
  • From the main method, do the following −
  • ret := 0, head := new node
  • sort the words array based on their length
  • n := size of words
  • for i in range 0 to n – 1
    • temp := insertNode(head, words[i])
    • if temp is non 0, then ret := ret + temp + 1
  • return ret.

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
struct Node{
   map <char, Node*> m;
};
class Solution {
   public:
   static bool cmp(string a, string b){
      return a.size() > b.size();
   }
   int insertNode(Node* head, string s){
      Node* curr = head;
      bool flag = false;
      for(int i = s.size() - 1; i >= 0; i--){
         char x = s[i];
         if(!curr->m[x]){
            flag = true;
            curr->m[x] = new Node();
         }
         curr = curr->m[x];
      }
      return flag? (int)s.size() : 0;
   }
   int minimumLengthEncoding(vector<string>& words) {
      int ret = 0;
      Node* head = new Node();
      sort(words.begin(), words.end(), cmp);
      int n = words.size();
      for(int i = 0; i < n; i++){
         int temp= insertNode(head, words[i]);
         if(temp){
            ret += (temp + 1);
         }
      }
      return ret;
   }
};
main(){
   vector<string> v = {"time", "me", "bell"};
   Solution ob;
   cout << (ob.minimumLengthEncoding(v));
}

Input

["time", "me", "bell"]

Output

10
raja
Published on 04-May-2020 14:31:23
Advertisements