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
- 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