Easy string | Practice | GeeksforGeeks

You are given the string S . Compress the string when lower and upper cases are the same. In compressed string characters should be in lowercase.
Example 1:
Input: S = "aaABBb"
Output: "3a3b"
Explanation: As 'a' appears 3 times
consecutively and 'b' also 3 times,
and 'b' and 'B' considered as same. 

â€‹Example 2:
Input: S = "aaacca"
Output: "3a2c1a"
Explanation: As 'a' appears 3 times
consecutively and 'c' also 2 times,
and then 'a' 1 time.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function transform() which takes the string S as inputs and returns the compressed string.

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ |S| ≤ 2 * 105
S contains only lowercase and uppercase characters.
0
csedeepak5 days ago
string transform(string S){   //complete the function here   string ans="";   for(int i=0; i<S.length(); i++){        char ch=tolower(S[i]);        int cnt=0;        while(ch==tolower(S[i])){            cnt++;            i++;        }        i--;        ans += to_string(cnt)+ch;   }   return ans;} 
+1
jayanthnalluri3531 month ago
string transform(string S){   //conversion of all the character into lower cases   for(int i=0;i<S.length();++i){       S[i]=S[i] | ' ';   }  stack<pair<char,int>> s;   string ans="";   s.push(make_pair(S[0],1));   for(int i=1;i<S.length();++i) {       if(S[i]==s.top().first) s.top().second++;       else{           ans=ans+to_string(s.top().second)+s.top().first;           s.pop();           s.push(make_pair(S[i],1));       }   }   return ans+to_string(s.top().second)+s.top().first;}
0
arpitkatiyar091972 months ago
USING STACK IN JAVA 
 
Stack<Character> stk=new Stack<>();       StringBuilder strb=new StringBuilder();       S=S.toLowerCase(); // SOLUTION BY arpitkatiyar09197       for(int i=S.length()-1;i>=0;i--)       {           stk.push(S.charAt(i));       }              while(!stk.isEmpty())       {           char ch=stk.peek();             stk.pop();           int count=1;                      while(stk.size()>0 && ch==stk.peek())           {               count++;               stk.pop();           }                        strb.append((count+"")+ch);       }              return strb.toString();       
0
arpitkatiyar091972 months ago
Using String Builder
JAVA SOLUTION
   class Solution {    String transform(String S)    {        S=S.toLowerCase();    /// Solution by arpitkatiyar09197         int count=1;       StringBuilder strb=new StringBuilder();       int i=0;       for(i=1;i<S.length();i++)       {           if(S.charAt(i)==S.charAt(i-1))           {               count++;           }                  else           {               strb.append((count+"")+S.charAt(i-1));               count=1;           }       }              if(i-1==S.length()-1)       {           strb.append((count+"")+S.charAt(i-1));       }              return strb.toString();
0
vrajeshmodi993 months ago
Time=0.4
 
string transform(string s){
    stack<int>sti;
    stack<char>stc;
    int k=s.size();
    
    for(int i=0;i<k;i++){
        int n;
       if(s[i]>='a' && s[i]<='z'){
            n = s[i] - 'a';
       }
             
       else  //(s[i]>='A' && s[i]<='Z')
       { 
          n = s[i]-'A';
       }
       
       if(stc.empty()){
           sti.push(1);
           n=n+'a';
           stc.push((char)n);
       }
       
       else if(stc.top()-'a' == n){
           sti.top()++;
       }
       else //(stc.top()-'a' != n)
       {
            sti.push(1);
            n=n+'a';
            stc.push((char)n);
       }
        
    }
   
    string str ="";
    while(!stc.empty()){
        string a = to_string(sti.top());
        sti.pop();
        str = a+stc.top()+str;
        stc.pop();
    }
    return str;
}
0
snipperwolf3 months ago
def transform(self, S):       # code here       for i in S:           if i.isupper():               S=S.replace(i,i.lower())       stack,l=[],[]       for i in range(len(S)):           if stack:               if stack[-1]!=S[i]:                   l.append(len(stack))                   l.append(stack.pop())                   stack=[]                   stack.append(S[i])               else:                   stack.append(S[i])           else:               stack.append(S[i])       if stack:           l.append(len(stack))           l.append(stack.pop())       return "".join([str(i) for i in l])
0
rathoredivya1503 months ago
string transform(string S){int count;  stack<char>s;   for(int i=S.size()-1;i>=0;i--)   {       if(S[i]>='A'&&S[i]<='Z')       {           S[i]=S[i]+32;       }       s.push(S[i]);          }string g;   while(s.size()>0)   {       char ch=s.top();       s.pop();       count=1;       if(s.size()>0&&s.top()==ch)       {       while(s.size()>0&&s.top()==ch)       {           count++;           s.pop();       }
       }               g+=to_string(count);               g+=ch;}
         return g;} 
0
nrk7863 months ago
string transform(string S){   //complete the function here   for(int i=0;i<S.size();i++){       if(S[i]>='A' and S[i]<='Z')           S[i]=S[i]+32;   }   S+=" ";   stack<char> s;   s.push(S[0]);   string x="";   for(int i=1;i<S.length();i++){       if(S[i]==s.top())       s.push(S[i]);       else{           char c=s.top();           int k=s.size();           while(!s.empty())           s.pop();           x+=to_string(k);           x+=c;           s.push(S[i]);       }   }   return x;}
+1
praveenkumar1391523 months ago
   def transform(self, S):       S=S.lower()       k=""       l=[]       l.append(S[0])       for i in S[1:]:           if l[0]==i:               l.append(i)           else:               k=k+str(len(l))+l[0]               l.clear()               l.append(i)       return k+str(len(l))+l[0]                      
+1
vg714 months ago
string transform(string s){   //complete the function here   int n=s.size();   transform(s.begin(), s.end(), s.begin(), ::tolower);   string output="";   int i=0;   int j=i+1;   int count=1;   if(n==1){      // string add=to_string(count);       output+=to_string(count);       output.push_back(s[0]);        return output;   }   while(i<n and j<n){       if(s[i]==s[j]){           count+=1;           j++;       }       else{          // string add=to_string(count);           output+=to_string(count);           output.push_back(s[i]);           i=j;           j++;           count=1;       }   }   if(i<n and j==n){       //string add=to_string(count);           output+=to_string(count);       output.push_back(s[i]);   }   return output;} 
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.