Skip to content
Related Articles

Related Articles

Improve Article
Perform n steps to convert every digit of a number in the format [count][digit]
  • Last Updated : 16 Jun, 2021

Given a number num as a string and a number N. The task is to write a program which converts the given number num to another number after performing N steps. At each step, every digit of num will be written in the format [count][digit] in the new number, where count is the number of times a digit occurs consecutively in num. 

Examples: 

Input: num = “123”; n = 3 
Output: 1321123113 
For, n = 1: 123 becomes 1 time 1, 1 time 2, 1 time 3, hence number 111213 
For, n = 2: 3 times 1, 1 time 2, 1 time 1, 1 time 3, hence number 31121113 
For, n = 3: 1 time 3, 2 times 1, 1 time 2, 3 times 1, 1 time 3, hence number 1321123113 

Input: num = “1213”; n = 1 
Output: 11121113 

Approach: Parse the string’s characters as a single digit and maintain a count for that digit till a different digit is found. Once a different digit is found, add the count of the digit to the new string and number to it. Once the string is parsed completely, recur for the function again with this new string till n steps are done. 



Below is the implementation of the above approach:  

C++




// C++ program to convert number
// to the format [count][digit] at every step
#include <bits/stdc++.h>
using namespace std;
 
// Function to perform every step
void countDigits(string st, int n)
{
 
    // perform N steps
    if (n > 0) {
        int cnt = 1, i;
        string st2 = "";
 
        // Traverse in the string
        for (i = 1; i < st.length(); i++) {
            if (st[i] == st[i - 1])
                cnt++;
            else {
                st2 += ('0' + cnt);
                st2 += st[i - 1];
                cnt = 1;
            }
        }
 
        // for last digit
        st2 += ('0' + cnt);
        st2 += st[i - 1];
 
        // recur for current string
        countDigits(st2, --n);
    }
 
    else
        cout << st;
}
 
// Driver Code
int main()
{
 
    string num = "123";
    int n = 3;
 
    countDigits(num, n);
 
    return 0;
}

Java




// Java program to convert number
// to the format [count][digit] at every step
class GFG
{
 
    // Function to perform every step
    public static void countDigits(String st, int n)
    {
 
        // perform N steps
        if (n > 0)
        {
            int cnt = 1, i;
            String st2 = "";
 
            // Traverse in the string
            for (i = 1; i < st.length(); i++)
            {
                if (st.charAt(i) == st.charAt(i - 1))
                    cnt++;
                else
                {
                    st2 += ((char) 0 + (char) cnt);
                    st2 += st.charAt(i - 1);
                    cnt = 1;
                }
            }
 
            // for last digit
            st2 += ((char) 0 + (char) cnt);
            st2 += st.charAt(i - 1);
 
            // recur for current string
            countDigits(st2, --n);
        }
        else
            System.out.print(st);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String num = "123";
        int n = 3;
        countDigits(num, n);
    }
}
 
// This code is contributed by
// sanjeev2552

Python3




# Python program to convert number
# to the format [count][digit] at every step
 
# Function to perform every step
def countDigits(st, n):
 
    # perform N steps
    if (n > 0) :
        cnt = 1
        i = 0
        st2 = ""
        i = 1
         
        # Traverse in the string
        while (i < len(st) ) :
            if (st[i] == st[i - 1]):
                cnt = cnt + 1
            else :
                st2 += chr(48 + cnt)
                st2 += st[i - 1]
                cnt = 1
            i = i + 1
 
        # for last digit
        st2 += chr(48 + cnt)
        st2 += st[i - 1]
 
        # recur for current string
        countDigits(st2, n - 1)
        n = n - 1;
 
    else:
        print(st)
 
# Driver Code
 
num = "123"
n = 3
 
countDigits(num, n)
 
# This code is contributed by Arnab Kundu

C#




// C# program to convert number
// to the format [count][digit] at every step
using System;
class GFG
{
 
// Function to perform every step
public static void countDigits(string st, int n)
{
 
    // perform N steps
    if (n > 0)
    {
        int cnt = 1, i;
        string st2 = "";
 
        // Traverse in the string
        for (i = 1; i < st.Length; i++)
        {
            if (st[(i)] == st[(i - 1)])
                cnt++;
            else
            {
                st2 += ((char) 0 + (char) cnt);
                st2 += st[(i - 1)];
                cnt = 1;
            }
        }
 
        // for last digit
        st2 += ((char) 0 + (char) cnt);
        st2 += st[(i - 1)];
 
        // recur for current string
        countDigits(st2, --n);
    }
    else
        Console.Write(st);
}
 
// Driver Code
public static void Main()
{
    string num = "123";
    int n = 3;
    countDigits(num, n);
}
}
 
// This code is contributed by
// Code_Mech.

Javascript




<script>
 
// Javascript program to convert number
// to the format [count][digit] at every step
 
// Function to perform every step
function countDigits(st, n)
{
     
    // Perform N steps
    if (n > 0)
    {
        let cnt = 1, i;
        let st2 = "";
 
        // Traverse in the string
        for(i = 1; i < st.length; i++)
        {
            if (st[i] == st[i - 1])
                cnt++;
            else
            {
                st2 += String.fromCharCode(
                    '0'.charCodeAt() + cnt);
                st2 += st[i - 1];
                cnt = 1;
            }
        }
 
        // For last digit
        st2 += String.fromCharCode(
            '0'.charCodeAt() + cnt);
        st2 += st[i - 1];
 
        // Recur for current string
        countDigits(st2, --n);
    }
    else
        document.write(st);
}
 
// Driver code
let num = "123";
let n = 3;
 
countDigits(num, n);
 
// This code is contributed by decode2207
 
</script>
Output: 
1321123113

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :