Cocubes Coding Questions – 7

You’re given a function –

char *CompressString(char* str);

The function accepts a string as an argument that may contain repetitive characters. Implement the function to modify and return the input string, such that each character once, along with the count of consecutive occurrence. Do not append count if the character occurs only once.

Note – 

  • The string will only contain lowercase English Alphabets
  • If you have to manipulate the input string in place you cant use another string

Assumption – 

No character will occur consecutively more than 9 times.

Example – 

input – aaaaabbbccccccccdaa

OutPut – a4b3c8da2

 

Please write the code in the comments in all languages it will be added here later –

17 comments on “Cocubes Coding Questions – 7”


  • Shahla

    //cpp solution
    int main() {
    string s;
    cin>>s;

    int cnt=0;
    int i=0,j=0;
    while( i < s.size() && j < s.size() )
    {
    if( cnt==0 ) cout<<s[i];

    if( s[i]==s[j] )
    {
    cnt++ ;
    j++ ;
    }
    else
    {
    cout<<cnt ;
    cnt=0 ;
    i=j ;
    }
    }

    return 0 ;
    }


  • Vignesh

    import java.util.*;
    public class stroccur{
    public static void main(String args[]){
    Scanner s=new Scanner(System.in);
    String st=s.next();
    String ans=””;
    for(int i=0;i1)
    ans+=c;
    }
    System.out.println(ans);
    }
    }


    • Vignesh

      import java.util.*;
      public class stroccur{
      public static void main(String args[]){
      Scanner s=new Scanner(System.in);
      String st=s.next();
      String ans=””;
      for(int i=0;i1)
      ans+=c;
      }
      System.out.println(ans);
      }
      }


  • Subhasis

    import java.util.*;
    class Main
    {
    public static void compress(String s)
    {
    HashMap map = new HashMap();
    for(int i = 0; i System.out.print(“” + k + “” + (v == 1? “” : v)));
    }

    public static void main (String[] args)
    {
    Scanner in = new Scanner(System.in);
    String s = in.nextLine();
    compress(s);
    }
    }


  • Shruti

    int c=1;
    char x;
    String s=”aacbaaad”;
    s=s+” “;
    for(int i=0;i<s.length()-1;i++){
    x=s.charAt(i);
    if(x!=s.charAt(i+1)){
    if(c!=1){
    System.out.print(x+""+c);
    }
    else {
    System.out.print(x);
    }
    c=1;
    }
    else{
    c++;
    }

    }


  • Ananya

    import java.util.*;
    public class Ncr1 {
    public static void main(String[] args) {
    Scanner in=new Scanner(System.in);
    String s=in.nextLine();
    ArrayList al=new ArrayList();
    int flag=0;
    int cnt=1;
    for(int i=0;i1){
    al.add(cnt);
    }
    if(cnt==1){
    al.add(s.charAt(i));
    }
    flag=0;
    cnt=1;
    }
    }
    if(s.charAt(s.length()-1)==s.charAt(s.length()-2)){
    al.add(cnt);
    }else{
    al.add(s.charAt(s.length()-1));
    }
    for(int i=0;i<al.size();i++){
    System.out.print(al.get(i));
    }
    System.out.println("");
    }
    }


  • Mohd Wael

    public class Main {
    public static void main(String[] args) {
    try(Scanner sc = new Scanner(System.in);){
    StringBuilder sb = new StringBuilder(sc.nextLine());
    int count = 1;
    char current= sb.charAt(0),next;
    for(int i = 1; i <sb.length(); i++){
    next=sb.charAt(i);
    if(next==current) {
    sb.deleteCharAt(i);
    i=i-1;
    count++;
    }
    else {
    sb.insert(i,count);
    count=1;
    current=next;
    i=i+1;
    }
    }
    sb.append(count);
    System.out.println(sb);
    }
    }
    }


  • Mayur

    //solution in cpp, without using inbuilt functions
    #include
    using namespace std;
    string tostring(int c)
    {
    int count = 0;
    int cc = c;
    while(cc)
    {
    count+=1;
    cc = cc/10;
    }

    string str = “”;
    int arr[count];
    for(int i = count-1;i>=0;i–)
    {
    arr[i] = (c%10) + 48;

    c = c/10;
    }
    for(int i =0;i> str;

    string str2;
    int f = 0;
    int c = 0;

    for(int i = 0;i<str.length() -1;i++)
    {

    if(str[i] == str[i+1])
    {
    if(f == 0)
    {
    str2 += str[i];
    }
    f = 1;
    c += 1;
    }
    if(str[i] != str[i+1] || i == str.length() -2)
    {
    str2 += tostring(c+1);
    f = 0;
    c = 0;
    }

    }
    cout<<str2;

    }


  • nikita gupta

    public static void main(String[] args) {
    String s=””,s1=”aaaaabbbccccccccdaa”;
    int n=s1.length();
    char c=s1.charAt(0);int count=1;
    for(int i=1;i<n;i++)
    {
    if(c==s1.charAt(i)){
    count++;
    }
    else{
    s+=c;
    s+=count;
    c=s1.charAt(i);

    count=1;
    }
    }
    s+=c;
    s+=count;
    System.out.println(s);

    }


  • Drastic Muna

    package com.sudhir.strings;

    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.Set;

    public class RunLength {
    static void printOccurence(String str) {
    char[] ch = str.toCharArray();
    Map lhMap = new LinkedHashMap();
    for (Character chArray : ch) {
    if (lhMap.containsKey(chArray)) {
    lhMap.put(chArray, lhMap.get(chArray) + 1);
    } else {
    lhMap.put(chArray, 1);
    }
    }
    for (Map.Entry entry : lhMap.entrySet()) {
    if (entry.getValue() == 1) {
    lhMap.remove(entry.getKey());

    }
    }
    System.out.println(lhMap);

    }

    public static void main(String[] args) {
    String str = “sssrrrruuttthhhi”;
    printOccurence(str);
    }

    }


  • prince

    n=input()
    l=len(n)
    print(l)

    m=1
    for i in range(l-1):
    if(n[i]==n[i+1]):
    m=m+1
    else:
    if(m>1):
    print(n[i]+str(m),end=”)
    m=1
    else:
    print(n[i],end=”)
    m=1

    if(n[l-2]==n[l-1]):
    print(n[l-1]+str(m),end=”)
    else:
    print(n[l-1],end=”)


  • Anjali

    import java.util.*;
    class Compress{
    public static void main(String[] args){
    String str=”aaaabbbccccccccd”;
    int asum=str.lastIndexOf(‘a’)-str.indexOf(‘a’)+1;
    int bsum=str.lastIndexOf(‘b’)-str.indexOf(‘b’)+1;
    int csum=str.lastIndexOf(‘c’)-str.indexOf(‘c’)+1;
    int dsum=str.lastIndexOf(‘d’)-str.indexOf(‘d’)+1;
    System.out.println(“a”+asum+”b”+bsum+”c”+csum+”d”+dsum);

    }

    }


      • Munmun Singh

        public class RunLength_Encoding {
        public static void printRLE(String str)
        {
        int n = str.length();
        for (int i = 0; i < n; i++) {

        // Count occurrences of current character
        int count = 1;
        while (i < n – 1 &&
        str.charAt(i) == str.charAt(i + 1)) {
        count++;
        i++;
        }

        // Print character and its count
        System.out.print(str.charAt(i));
        System.out.print(count);
        }
        }

        public static void main(String[] args)
        {
        String str = "wwwwaaadexxxxxxywww";
        printRLE(str);
        }
        }

        output:
        w4a3d1e1x6y1w3