Given a number as string, remove recurring digits from the given string. The changes must be made in-place. Expected time complexity O(n) and auxiliary space O(1).
Input: num = "1299888833" Output: num = "12983" Input: num = "1299888833222" Output: num = "129832"
We strongly recommend you to minimize your browser and try this yourself first
This problem is similar to Run Length Encoding.
Let num be input number represented as character array 1) Initialize index of modified string 'j' as 0. 2) Traverse input string and do following for every digit num[i]. a) Copy current character 'num[i]' to 'num[j]' and increment i & j. b) Keep incrementing i while num[i] is same as previous digit. 3) Add string termination character at 'num[j]'
Below is the implementation of above algorithm.
Modified number is 12983
This article is contributed by Priyanka. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Remove characters from the first string which are present in the second string
- Remove duplicates from a given string
- Given a number, find the next smallest palindrome
- Remove "b" and "ac" from a given string
- Recursively remove all adjacent duplicates
- Find Excel column name from a given column number
- Print all possible words from phone digits
- How to turn off a particular bit in a number?
- Given a binary string, count number of substrings that start and end with 1.
- Factorial of a large number
- Function to find Number of customers who could not get a computer
- Build Lowest Number by Removing n digits from a given number
- Converting Decimal Number lying between 1 to 3999 to Roman Numerals
- Remove spaces from a given string
- Transform One String to Another using Minimum Number of Given Operation
- Minimum number of bracket reversals needed to make an expression balanced
- Form minimum number from given sequence
- 1's and 2's complement of a Binary Number
- Minimum number of palindromic subsequences to be removed to empty a binary string
- Find all strings formed from characters mapped to digits of a number