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.
Input: num = “123”; n = 3
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
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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Convert a number of length N such that it contains any one digit at least 'K' times
- Minimum steps to convert one binary string to other only using negation
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Convert time from 24 hour clock to 12 hour clock format
- Find minimum number of steps to reach the end of String
- Minimum number of steps needed to remove the substring K from given string
- Sum of digit of a number using recursion
- Find the Number which contain the digit d
- Program to Convert BCD number into Decimal number
- Find the frequency of a digit in a number
- Program to delete Nth digit of a Number
- Find the minimum number possible by changing at most one digit
- Count the occurrence of digit K in a given number N using Recursion
- Print multiples of Unit Digit of Given Number
- C++ Program for Largest K digit number divisible by X
- C++ Program for Smallest K digit number divisible by X
- Perform the given queries on the rooted tree
- Print all 3 digit repeating numbers in a very large number
- Print all K digit repeating numbers in a very large number
- Add the given digit to a number stored in a linked list using recursion
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.