![]() |
VOOZH | about |
Given a String list, append to String i or j value depending on Kth index value.
Input : test_list = ["geeksforgeeks", "best", "for", "geeks"], K = 2, N = 'e', i, j = "@@", ".."
Output : ['geeksforgeeks..', 'best@@', 'for@@', 'geeks..']
Explanation : geeksforgeeks and geeks having similar 2nd occ. value as 'e', hence gets appended by "..".
Input : test_list = ["giiksforgeeks", "bst", "for", "geeks"], K = 2, N = 'e', i, j = "@@", ".."
Output : ['giiksforgeeks@@', 'best@@', 'for@@', 'geeks@@']
Explanation : No values with K value 'e', all appended by @@.
Method #1: Using loop
This is a brute way to solve this problem, we check for each string's Kth index, if found to be N, then i value is appended else j is appended.
The original list : ['geeksforgeeks', 'best', 'for', 'geeks'] The resultant List : ['geeksforgeeks**', 'best##', 'for##', 'geeks**']
Time complexity: O(n), where n is the length of the test_list. The loop takes O(n) time.
Auxiliary Space: O(n), extra space of size n is required.
Method #2: Using list comprehension
This solves this problem in a similar manner, just the difference being, it's a shorthand and can be used as a one-liner approach to solve this problem.
The original list : ['geeksforgeeks', 'best', 'for', 'geeks'] The resultant List : ['geeksforgeeks**', 'best##', 'for##', 'geeks**']
The Time and Space Complexity for all the methods is the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using map() with lambda function
The original list : ['geeksforgeeks', 'best', 'for', 'geeks'] The resultant List : ['geeksforgeeks**', 'best##', 'for##', 'geeks**']
Time Complexity: O(N)
Auxiliary Space: O(N)