Problem Statement: You have given a string. There can have multiple special characters inside the string. Write a program to reverse the given string. The position of the special characters should not be changed.
Example:
Input: 'abc/defgh$ij' Output: 'jih/gfedc$ba'
This question was asked in the Oracle interview.
Here is a simple algorithm to solve this problem to reverse string without affecting special characters
strSample='abc/defgh$ij' #convert string into list listSample=list(strSample) i=0 j=len(listSample)-1 while i<j: if not listSample[i].isalpha(): i+=1 elif not listSample[j].isalpha(): j-=1 else: #swap the element in the list #if both elements are alphabets listSample[i], listSample[j]=listSample[j], listSample[i] i+=1 j-=1 #convert list into string #by concatinating each element in the list strOut=''.join(listSample) print(strOut)
Output:
jih/gfedc$ba
An isAlpha()
is the character method that returns true if the character is an alphabet. Otherwise (if it is a special character), it returns false.
In Python, we can reverse the string with a single line of code. But, this is a special case where we don’t want to change the position of the special characters.
Similarly, you can solve this problem in C/C++ or Java.
We are traversing each character in the string at once. In the worst case, the time complexity is O(n)
, where n is the size of the given string.
We can do the in-place swapping of the characters of the string. So, it does not require any extra space. (In the case of Python, it’s not possible to swap the characters inside the string. So, we are obligated to use the list. This causes extra memory space.)
Other Python Coding Questions for Practice:
This is the simple solution to reverse string without affecting special characters. Practice solving more of such coding questions to improve your programming skills.
Hi,
How to do code for below one?
Sample Input:
In this case, the words of the given string are reversed. Here is how you do this.
1. Split the words from the strings
2. Loop over all the words in the string
3. Reverse each word and concatenate it to the output string.
Let me know if it is not clear.
Hi Abishek, do you got the solution for that?
yes not cleared
but o/p is not coming as expected
Rahul, the Program you have written is to reverse each individual word in the sentence.
It returns.
Hi Abhishek,
below is code which work for you:
Output:
An additional space gets appended at the end of the reversed string: “gnirtS; 2eb desrever… “
very helpful thanks
Hi,
How do I include assert in the main() condition which must succeed upon error?
I did not get your comment. You can refer to Python assert statement.
Interesting. But, it will consume extra memory for strings and lists.
Thank you for your response. Can you please guide in memory management and time complexity?
In your example, you have created an extra list (list1) of leght n. So the space complexity has increased to O(n).
As you looping over the list only once, the time complexity is O(n) which is very efficient.
How to get this output “gnirtS; eb2 desrever…” where the condition is:
1. Reverse each word in the input string.
2. The order of the words will be unchanged.
3. A word is made up of letters and/or numbers.
4. Other characters (spaces, punctuation) will not be reversed.
how to collect words in advance and add them to the list already folded?
The main check must pass.
The code you shared is to reverse the string. We want to reverse the string without changing the positions of special characters.
Can I reverse the string without changing the positions of special characters and without using string function like isalpha(), isalnum?
Yes. It is possible doing it manually instead of using a built-in function.
Bro how do we revere the string without changing the position.
Ex :
Brother, I have already explained it.
Refer this tutorial here- https://www.csestack.org/python-program-reverse-each-word-string/
let input=”my name is rahul”
let output=input.split(” “)
let final=””
for (i=0;i=0; j–){
reverse += output[i][j]
}
final+= reverse
}
console.log(final)
pls, answer this code in java language.