• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reversing string containing [] which carry special meaning.  RSS feed

 
Kashif Ahmed Khan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a question. I have to reverse a string. The requirements are as follows.

1. From standard String.java class, only length() and charAt(int index) can be used.
2. Other Standard Java classes can be used.
3. If a string contains '[' and ']' then the string inside that shouldn't be reversed. However, if there are multiple instances then the pair of '[' ']' coming inside another '[' ']' will be reversed.
4. It should be thread safe and if there are not matching pairs of '[' ']' then some exception should be thrown.
5. The resulting reversed string should remove '[' and ']'.

e.g. If we have the following string.
Input = "This string [will not be reversed but [this inside here will be reversed] and this will not be reversed ] however, this part is reversed".
Output = "desrever si trap, revewoh will not be reversed but desrever eb lliw ereh rdisne sith will not be reversed gnirts sith".

How can we solve this. All ideas will be appreciated.

Regards
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Step 1: Forget that computers exist.

You need to figure out how to solve this "manually". And more importantly, you need to express that solution in very clear, precise, simple steps. Do that before you even think about writing a line of Java code.

Write down your solution in whatever combination of English/your native language/pseudocode is easiest for you. If you do this part correctly, it will be pretty simple to translate those steps to Java. If it's not, then your steps were not precise enough or simple enough.


 
Kashif Ahmed Khan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have been able to solve it. I was wondering if this could be made better and more cleaner than it is. Choice of some other data structure or way of solving would indeed be interesting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!