Given a string with words, concatenate the Kth index of each word.
Input : test_str = ‘neveropen bestneveropen’, K = 3
Output : ktk
Explanation : 3rd index of “neveropen” is k, “best” has ‘t’ as 3rd element.Input : test_str = ‘neveropen bestneveropen’, K = 0
Output : gbg
Method #1 : Using join() + list comprehension + split()
In this, we perform the task of splitting to get all the words and then use list comprehension to get all Kth index of words, join() is used to perform concatenation.
Step-by-step approach:
- Use the split() method to split the input string into a list of words.
- Use a list comprehension to iterate over each word in the list obtained in step 4 and select the Kth character of each word.
- Use the join() method to concatenate the Kth characters of all the words in the list obtained in step 5.
- Store the concatenated string in a variable called res.
- Print the concatenated string using the print() function and string concatenation.
Below is the implementation of the above approach:
Python3
# initializing string test_str = 'neveropen best forneveropen' # printing original string print ( "The original string is : " + test_str) # initializing K K = 2 # joining Kth index of each word res = ''.join([sub[K] for sub in test_str.split()]) # printing result print ( "The K joined String is : " + str (res)) |
The original string is :neveropen best forneveropen The K joined String is : esre
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using loop + join()
Step-by-step approach:
- Initializes an integer variable K.
- Creates an empty list temp.
- Uses the split() method to split the test_str variable into a list of individual words. It then iterates over each word in the list using a for loop.
- For each word, the program appends the Kth character of the word to the temp list using the append() method.
- Once all words have been processed, the program joins the elements of temp together into a single string using the join() method and assigns the result to a variable named res.
- Finally, the program prints the resulting string using the print() function with the message “The K joined String is : ” concatenated with the string representation of the res variable.
Below is the implementation of the above approach:
Python3
# initializing string test_str = 'neveropen best forneveropen' # printing original string print ( "The original string is : " + test_str) # initializing K K = 2 # getting Kth element of each word temp = [] for sub in test_str.split(): temp.append(sub[K]) # joining together res = ''.join(temp) # printing result print ( "The K joined String is : " + str (res)) |
The original string is :neveropen best forneveropen The K joined String is : esre
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using map() + lambda function + str.join() + str.split()
Step-by-step approach:
- Initialize the variable “K” with the index of the character to be extracted from each word.
- Split the input string into a list of words using the “split()” method and store it in the variable “words”.
- Use the “map()” function to apply a lambda function to each word in the list “words”.
- The lambda function takes a word as input and returns the Kth character of that word.
- Use the “join()” method to join the resulting list of characters into a single string.
- Print the resulting string.
Below is the implementation of the above approach:
Python3
# initializing string test_str = 'neveropen best forneveropen' # printing original string print ( "The original string is : " + test_str) # initializing K K = 2 # getting Kth element of each word res = ''.join( map ( lambda x: x[K], test_str.split())) # printing result print ( "The K joined String is : " + str (res)) |
The original string is :neveropen best forneveropen The K joined String is : esre
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.