Array ( [0] => [1] => questions [2] => Strings [3] => Array-of-strings )
Given a string K. You have to find the reverse of given string.
Given a string
Return the reverse of the string
Inquisitive
evitisiuqnI
Institute
etutitsnI
Knowledge
egdelwonK
Login to see Discussion
Approach 1: Using loop
step-i)create an empty string answer
step-ii)Traverse the given string from back and keep appending characters to answer
step-iii) return answer
Time Complexity: O(n)
Space Complexity: O(n)
Approach 2: Using list
step-i)Convert given string to character array ( for traversal purpose which you will see in next steps)
step-ii)Create a list of characters
step-iii)Traverse the character array and keep adding characters to the list
step-iv) Reverse the list
step-v)create a empty string
step-vi) Now traverse the list and append characters to the string created
step-vii) return the string
Time Complexity: O(n)
Space Complexity: O(n)
Approach 3: Using buitin method of reverse
step-i) copy given string to the string builder
step-ii) reverse the string builder by calling reverse method
step-iii) return the reversed string
Time Complexity: O(n)
Space Complexity: O(n)
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution