Given a String, the task is to write a Python program to increment the number which is at end of the string.
Input : test_str = ‘Lazyroar006’
Output : Lazyroar7
Explanation : Suffix 006 incremented to 7.Input : test_str = ‘Lazyroar007’
Output : Lazyroar8
Explanation : Suffix 007 incremented to 8.
Method #1 : Using findall() + join() + replace()
In this, strategy we perform the task of finding number using findall(), then perform the task of separating numeric string and prefix string, then an increment of a numeric string is performed. At last, the string is joined to get a prefix followed by an incremented number.
Python3
# Python3 code to demonstrate working of # Increment Suffix Number # Using findall() + join() + replace() import re # initializing string test_str = 'Lazyroar006' # printing original string print ( "The original string is : " + str (test_str)) # getting suffix number reg = re. compile (r '[ 0 - 9]' ) mtch = reg.findall(test_str) # getting number num = ''.join(mtch[ - 3 : ]) pre_str = test_str.replace(num, '') # Increment number add_val = int (num) + 1 # joining prefix str and added value res = pre_str + str (add_val) # printing result print ( "Incremented numeric String : " + str (res)) |
Output:
The original string is : Lazyroar006 Incremented numeric String : Lazyroar61
Method #2 : Using sub() + group() + zfill()
In this, we perform the task of grouping numbers using group() and incrementing, zfill() is used for task of filling the required leading values in numerical. The sub() is used to find the numerical part of strings.
Python3
# Python3 code to demonstrate working of # Increment Suffix Number # Using sub() + group() + zfill() import re # initializing string test_str = 'Lazyroar006' # printing original string print ( "The original string is : " + str (test_str)) # fstring used to form string # zfill fills values post increment res = re.sub(r '[0-9]+$' , lambda x: f "{str(int(x.group())+1).zfill(len(x.group()))}" , test_str) # printing result print ( "Incremented numeric String : " + str (res)) |
Output:
The original string is : Lazyroar006 Incremented numeric String : Lazyroar007
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Method #3 : Using string slicing and addition
Python3
# initializing string test_str = 'Lazyroar006' # printing original string print ( "The original string is : " + str (test_str)) suffix = int (test_str[ - 3 :]) prefix = test_str[: - 3 ] # Increment number suffix + = 1 # joining prefix str and added value res = prefix + str (suffix).zfill( 3 ) # printing result print ( "Incremented numeric String : " + str (res)) #This code is contributed by Vinay Pinjala. |
The original string is : Lazyroar006 Incremented numeric String : Lazyroar007
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using isalpha(),replace() and slicing
Python3
# Python3 code to demonstrate working of # Increment Suffix Number # initializing string test_str = 'Lazyroar006' # printing original string print ( "The original string is : " + str (test_str)) x = test_str[:: - 1 ] res = "" for i in x: if i.isalpha(): break else : res + = i res = res[:: - 1 ] x = int (res) + 1 test_str = test_str.replace(res,"") test_str + = str (x) # printing result print ( "Incremented numeric String : " + str (test_str)) |
The original string is : Lazyroar006 Incremented numeric String : Lazyroar7
Time Complexity: O(n)
Auxiliary Space: O(n)
Method#5: Using Recursive method.
Here is the step-by-step algorithm for this code:
- Define a function increment_suffix(s) that takes in a string s, and increments the numeric suffix at the end of the string by 1.
- Check for the base case where the input string is empty. If the string is empty, return the string “1” to indicate that the suffix is 0.
- For the recursive case, check whether the last character in the string is a digit by calling the isdigit() string method.
- If the last character is a digit, compute the carry by adding 1 to the integer value of the last character, and then dividing by 10 to get the carry digit. The carry is added to the result of a recursive call to increment_suffix() on the substring s[:-1]. The incremented suffix digit is obtained by taking the sum of the last character and the carry, modulo 10.
- If the last character is not a digit, simply return the input string s.
- In the main code, initialize the test string test_str.
- Call the increment_suffix() function on the substring test_str[:-3] to get the incremented suffix without the last three digits. The last three digits are extracted from the original test_str using string slicing, and are incremented by 1 using integer addition. The zfill() method is then called on the resulting integer to ensure that it has three digits.
- Concatenate the result of the recursive call to increment_suffix() with the incremented suffix digits, and print the result.
Python3
# Python3 code to demonstrate working of # Increment Suffix Number def increment_suffix(s): if not s: return '1' if s[ - 1 ].isdigit(): carry = ( int (s[ - 1 ]) + 1 ) / / 10 return increment_suffix(s[: - 1 ]) + str (( int (s[ - 1 ]) + carry) % 10 ) return s # initializing string test_str = 'Lazyroar006' # printing original string print ( "The original string is : " + str (test_str)) res = increment_suffix(test_str[: - 3 ]) + str ( int (test_str[ - 3 :]) + 1 ).zfill( 3 ) # printing result print ( "Incremented numeric String : " + str (res)) #this code contributed by tvsk |
The original string is : Lazyroar006 Incremented numeric String : Lazyroar007
The time complexity of this method is O(n), where n is the length of the input string, because it processes each character of the string at most once. The worst-case time complexity occurs when the suffix has many digits, in which case the method makes many recursive calls. The best-case time complexity occurs when the suffix has only one digit, in which case the method makes only one call to increment_suffix().
The space complexity of this method is O(n), because it creates a new stack frame for each recursive call, which requires additional memory on the call stack. However, the maximum depth of the call stack is equal to the length of the input string, so the space complexity is proportional to the size of the input.