Given a String, replace ith index by K value.
Input : test_str = 'Lazyroar5Lazyroar', K = '7', i = 5 Output : 'Lazyroar7Lazyroar' Explanation : Element is 5, converted to 7 on ith index.
Input : test_str = 'Lazyroar5Lazyroar', K = '7', i = 6 Output : 'Lazyroar56eeks' Explanation : Element is g, converted to 7 on ith index.
Method #1: Using string slicing
In this, we perform the slicing of pre string, till i, and then add K, then add post values, using string slice method.
Python3
# Python3 code to demonstrate working of # Replace to K at ith Index in String # using string slicing # initializing strings test_str = 'Lazyroar5Lazyroar' # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = '4' # initializing i i = 5 # the replaced result res = test_str[: i] + K + test_str[i + 1 :] # printing result print ( "The constructed string : " + str (res)) |
The original string is : Lazyroar5Lazyroar The constructed string : Lazyroar4Lazyroar
Time Complexity: O(n) -> string slicing
Auxiliary Space: O(n)
Method #2 : Using join() + generator expression
In this, we perform the task of checking for ith index and conditionally appending K, using generator expression and convert the result into string using join().
Python3
# Python3 code to demonstrate working of # Replace to K at ith Index in String # using join() + generator expression # initializing strings test_str = 'Lazyroar5Lazyroar' # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = '4' # initializing i i = 5 # the replaced result res = ''.join(test_str[idx] if idx ! = i else K for idx in range ( len (test_str))) # printing result print ( "The constructed string : " + str (res)) |
The original string is : Lazyroar5Lazyroar The constructed string : Lazyroar4Lazyroar
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using replace() method
Python3
# Python3 code to demonstrate working of # Replace to K at ith Index in String # using string slicing # initializing strings test_str = 'Lazyroar5Lazyroar' # printing original string print ( "The original string is : " + str (test_str)) # initializing K K = '4' # initializing i i = 5 test_str = test_str.replace(test_str[i],K, 1 ) # printing result print ( "The constructed string : " + str (test_str)) |
The original string is : Lazyroar5Lazyroar The constructed string : Lazyroar4Lazyroar
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using list() and join() methods
Python3
# Python3 code to demonstrate working of # Replace to K at ith Index in String # Initializing strings test_str = 'Lazyroar5Lazyroar' # Printing original string print ( "The original string is : " + str (test_str)) # Initializing K K = '4' # Initializing i i = 5 x = list (test_str) x[i] = K res = "".join(x) # Printing result print ( "The constructed string : " + str (res)) |
The original string is : Lazyroar5Lazyroar The constructed string : Lazyroar4Lazyroar
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using bytearray()
- Initialize the string test_str and print it.
- Initialize the values of K and i.
- Convert the string to a bytearray using bytearray(test_str, encoding=’utf-8′). This creates a mutable bytearray object that can be modified in-place.
- Replace the character at index i with K by assigning K to test_str[i].
- Convert the bytearray back to a string using bytes.decode() method.
- Print the resulting string.
Python3
# Python3 code to demonstrate working of # Replace to K at ith Index in String # Initializing strings test_str = 'Lazyroar5Lazyroar' # Printing original string print ( "The original string is : " + str (test_str)) # Initializing K K = '4' # Initializing i i = 5 # Convert string to bytearray byte_str = bytearray(test_str, encoding = 'utf-8' ) # Replace character at index i with K byte_str[i] = ord (K) # Convert bytearray back to string res = byte_str.decode() # Printing result print ( "The constructed string : " + str (res)) |
The original string is : Lazyroar5Lazyroar The constructed string : Lazyroar4Lazyroar
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), where n is the length of the input string.
Method #6: Using string concatenation and string indexing
Step-by-step approach:
- Initialize the original string, K, and i variables.
- Create a new string variable to store the modified string.
- Loop through each character in the original string, adding each character to the new string until you reach the index i.
- Add the character K to the new string.
- Continue looping through the original string, starting at the index i+1, and adding each character to the new string.
- Print the new string.
Below is the implementation of the above approach:
Python3
# Initializing strings test_str = 'Lazyroar5Lazyroar' # Printing original string print ( "The original string is : " + str (test_str)) # Initializing K K = '4' # Initializing i i = 5 # Create a new string variable to store the modified string new_str = '' # Loop through each character in the original string for j in range ( len (test_str)): # If we have reached the index i, add the character K to the new string if j = = i: new_str + = K # Otherwise, add the current character to the new string else : new_str + = test_str[j] # Printing result print ( "The constructed string : " + str (new_str)) |
The original string is : Lazyroar5Lazyroar The constructed string : Lazyroar4Lazyroar
Time complexity: O(n), where n is the length of the original string.
Auxiliary space: O(n), where n is the length of the original string, since we are creating a new string to store the modified string.