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
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.
- Minimum digits to be removed to make either all digits or alternating digits same
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Remove repeated digits in a given number
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Different Ways to Remove all the Digits from String in Java
- Maximize the value of A by replacing some of its digits with digits of B
- Build Lowest Number by Removing n digits from a given number
- Smallest odd number with even sum of digits from the given number N
- Minimum and maximum number of digits required to be removed to make a given number divisible by 3
- Maximum number of given operations to remove the entire string
- Minimum number of steps needed to remove the substring K from given string
- Remove leading zeros from a Number given as a string
- Remove characters from given string whose frequencies are a Prime Number
- Find the last two missing digits of the given phone number
- Program to insert dashes between two adjacent odd digits in given Number
- Remove duplicates from a given string
- Remove "b" and "ac" from a given string
- Remove spaces from a given string
- Remove comments from a given C/C++ program
- Minimum characters to be replaced to remove the given substring