Array ( [0] => [1] => questions [2] => Basic [3] => Array-of-strings ) Strings | Array of strings | THE INQUISITIVE





Array of strings

LEVEL:Beginner

Description

Given a string K. You have to find the reverse of given string.

Input Format

Given a string

Output Format

Return the reverse of the string


Example 1:

Input
Inquisitive
Output
evitisiuqnI	
Example 2:

Input
Institute
Output
etutitsnI
Example 3:

Input
Knowledge
Output
egdelwonK

oops

Login to see Discussion




Approach

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.

oops

Login to see Solution