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 = ‘geeks006’
Output : geeks7
Explanation : Suffix 006 incremented to 7.
Input : test_str = ‘geeks007’
Output : geeks8
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
import re
test_str = 'geeks006'
print ( "The original string is : " + str (test_str))
reg = re. compile (r '[ 0 - 9]' )
mtch = reg.findall(test_str)
num = ''.join(mtch[ - 3 : ])
pre_str = test_str.replace(num, '')
add_val = int (num) + 1
res = pre_str + str (add_val)
print ( "Incremented numeric String : " + str (res))
|
Output:
The original string is : geeks006
Incremented numeric String : geeks61
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
import re
test_str = 'geeks006'
print ( "The original string is : " + str (test_str))
res = re.sub(r '[0-9]+$' ,
lambda x: f "{str(int(x.group())+1).zfill(len(x.group()))}" ,
test_str)
print ( "Incremented numeric String : " + str (res))
|
Output:
The original string is : geeks006
Incremented numeric String : geeks007
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
test_str = 'geeks006'
print ( "The original string is : " + str (test_str))
suffix = int (test_str[ - 3 :])
prefix = test_str[: - 3 ]
suffix + = 1
res = prefix + str (suffix).zfill( 3 )
print ( "Incremented numeric String : " + str (res))
|
Output
The original string is : geeks006
Incremented numeric String : geeks007
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using isalpha(),replace() and slicing
Python3
test_str = 'geeks006'
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)
print ( "Incremented numeric String : " + str (test_str))
|
Output
The original string is : geeks006
Incremented numeric String : geeks7
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
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
test_str = 'geeks006'
print ( "The original string is : " + str (test_str))
res = increment_suffix(test_str[: - 3 ]) + str ( int (test_str[ - 3 :]) + 1 ).zfill( 3 )
print ( "Incremented numeric String : " + str (res))
|
Output
The original string is : geeks006
Incremented numeric String : geeks007
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
25 Feb, 2023
Like Article
Save Article