Encrypt the string - 2 | Practice | GeeksforGeeks

You are given a string S. Every sub-string of identical letters is replaced by a single instance of that letter followed by the hexadecimal representation of the number of occurrences of that letter. Then, the string thus obtained is further encrypted by reversing it [ See the sample for more clarity ]. Print the Encrypted String.
Note: All Hexadecimal letters should be converted to Lowercase letters.
 
Example 1:
Input:
S = "aaaaaaaaaaa"
Output:
ba 
Explanation: 
aaaaaaaaaaa
Step1: a11 (a occurs 11 times)
Step2: a11 is ab [since 11 is b in
hexadecimal]
Step3: ba [After reversing]
Example 2:
Input:
S = "abc"
Output:
1c1b1a
Explanation: 
abc
Step1: a1b1c1
Step2: 1c1b1a [After reversing]
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function encryptString() which takes a String S and returns the answer.
 
Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)

Constraints
1 <= |S| <= 105
0
skumar2009200113 hours ago
 string ans="";       int count=1,i=0;       for( i=0;i<S.size();i++){           if(S[i]==S[i+1]){               count++;           }           else{               ans.push_back(S[i]);               int temp=count;               int r;               while (temp != 0)               {                   r = temp % 16;                   if (r < 10)                       ans.push_back(r+48);                   else                       ans.push_back(r+87);                   temp = temp / 16;                   count=1;               }           }       }       reverse(ans.begin(),ans.end());       return ans;   }
0
madwix4 days ago
//TRY THIS:
map<int, char> mp;
       mp[0]='0';
       mp[1]='1';
       mp[2]='2';
       mp[3]='3';
       mp[4]='4';
       mp[5]='5';
       mp[6]='6';
       mp[7]='7';
       mp[8]='8';
       mp[9]='9';
       mp[10]='a';
       mp[11]='b';
       mp[12]='c';
       mp[13]='d';
       mp[14]='e';
       mp[15]='f';
       int cnt=1, i, j, sum=0, len=S.size();
       string str="";
       for(i=0;i<len;i++){
           if(S[i]==S[i+1]){
               cnt++;
               continue;
           }
           str.push_back(S[i]);
           while(cnt>0){
               sum=cnt%16;
               str+=mp[sum];
               cnt=floor(cnt/16.0);
               
           }
           cnt=1;
       }
       reverse(str.begin(), str.end());
       return str;
0
mayankverma6271 month ago
Hi all. What's wrong with this code? I'm only learning to code.
map<int,char> mpp;       mpp[1]='1';       mpp[2]='2';       mpp[3]='3';       mpp[4]='4';       mpp[5]='5';       mpp[6]='6';       mpp[7]='7';       mpp[8]='8';       mpp[9]='9';       mpp[10]='a';       mpp[11]='b';       mpp[12]='c';       mpp[13]='d';       mpp[14]='e';       mpp[15]='f';              int count = 1;              string t="";              for(int i=0;i<S.size();++i){           if(S[i]==S[i+1]){               count++;           }           else{               t.push_back(S[i]);               t += to_string(mpp[count]);               count = 1;           }       }       reverse(t.begin(),t.end());       return t;
-1
prashantdahiya7113 months ago
    def encryptString(self, S):
        s=list(S)
        d={}
        for i in range(len(s)):
            if s[i] in d.keys():
                d[s[i]]+=1
            else:
                d[s[i]]=1
        for k,v in d.items():
            if(d[k]==10):
                d[k]="a"
            if(d[k]==11):
                d[k]="b"
            if(d[k]==12):
                d[k]="c"
            if(d[k]==13):
                d[k]="d"
            if(d[k]==14):
                d[k]="e"
            if(d[k]==15):
                d[k]="f"
        arr=[]
        for k,v in d.items():
            arr.append(k)
            arr.append(v)  
        h=arr.reverse()
        listToStr = ''.join(map(str, arr))
        return listToStr
-4
shubhambhosale04113 months ago
//one of the best solution
class Solution { public:   string encryptString(string S) {       // code here       string res="";       int n=S.size();       int cnt=1;       for(int i=0;i<n;i++)       {                                if(S[i]==S[i+1] )           {               cnt++;           }           else{                          res.push_back(S[i]);           if(cnt==10)           {               res.push_back('a');           }           else if(cnt==11)           {               res.push_back('b');           }           else if(cnt==12)           {               res.push_back('c');           }           else if(cnt==13)           {               res.push_back('d');           }           else if(cnt==14)           {               res.push_back('e');           }           else if(cnt==15)           {               res.push_back('f');           }           else{            //   char p=65+cnt;               char c = cnt+'0';               res.push_back(c);               cnt=1;           }           }       }       reverse(res.begin(),res.end());       return res;   }};
0
abythankachan3143 months ago
 def encryptString(self, S):       S=list(S)       list2=[]       list3=[]       list4=[]       count_s=''       for i in S:           if i not in list2:               list2.append(i)       for t in range(0,len(list2)):           list3.append(S.count(list2[t]))       for j in list3:           list4.append(hex(j).lstrip('0x').rstrip('L'))       for k in range(0,len(list2)):
       count_s=count_s+list2[k]+str(list4[k]).lower()       return count_s[ : :-1]
==============================
What is error in this, only 102 test case passed not all.
 
       
+5
anooptaliyan3 months ago
 //IMPORTANT NOTE: THEY  CONSIDER HEXADECIMAL VALUE AS A SINGLE UNIT  // because of single unit, hexadecimal value does not need to be reversed  //hex function to convert decimal value into hexadecimalstring hex(int n){     string s1;      while(n)   {       int rem=n%16;       if(rem>9)       {           s1+=97+rem-10;                  }       else       {           s1+=48+rem;                  }     n=n/16;     }               
   return s1;}      string encryptString(string s) {       // code here            string ans;            int n=s.length();       for(int i=0;i<s.length();i++)       {          int  count=1;          ans+=s[i];                                if(s[i]==s[i+1]&&i<n-1)           {               while(s[i]==s[i+1]&&i<n)               {                   count++;                   i++;                                  }                              if(count<10)               {                   ans+=48+count;                              }               else               {                   string temp;                   temp=hex(count);                   ans+=temp;               }                          }           else           {               ans+=49;                          }                  }       reverse(ans.begin(),ans.end());       cout<<ans;                 }}
0
kalrarishav175 months ago
just take hexstring as a unit while reversing the string.
0
sanjaysanjay12705 months ago
/////////Very easy Solution
  string encryptString(string str) {       reverse(str.begin(),str.end());       int i=0,count=1;string ss="";       while(i<str.length())       {           if(str[i]==str[i+1])           {               count++;           }           else           {                              stringstream s;               s<<hex<<count;               ss+=s.str()+str[i];               count=1;           }            i++;       }       return ss;
   }
0
jack11435 months ago
Executed in 0.0 sec
 string convert(int x){
      string ans;
      while(x){
          int r = x%16;
          x /= 16;
          if(r <= 9)ans.push_back(char('0' + r));
          else ans.push_back(char('a' + (r - 10)));
      }
      return ans;
  }
    string encryptString(string S) {
        // code here
        string ans;
        int count = 1, i = 0;
        S += '$';
        for(int j = 1;j < S.size();j++)
        {
            if(S[i] == S[j])count++;
            else
            {
                ans += S[i];
                ans += convert(count);
                i = j;
                count = 1;
            }
        }
        reverse(ans.begin(),ans.end());
        return ans;
    }
}; 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.