0
votes

I'm trying to write a code which has a string with a special character "," in it. It can have either one occurrence or multiple occurrences.

Now what I want to do is I want to replace every special character with characters [a-z]. Every single special character should be replaced with every character with characters [a-z].

After everytime the special characters are all replaced I will check the string for a property, if it satisfies that property, the program should terminate and the string will be returned.

For example take input as : hello,,how

I want to replace first "," with 'a' and second "," with 'a' first and then with a,b then with a,c ..... a,z till z,a......z,z. (If in this process a string turns out to obey my required property, that string will be returned)

The above example hello,,how would produce these strings - hellowaahow , hellowabhow, hellowachow..... hellowazhow, ...... hellozahow,hellowzbhow, hellozchow......hellozzhow. If any time in this process a string satisfies a property that string will be returned and program will exit

I don't even have an idea how to approach this problem, can anyone help me out with an efficient technique?

1
Are there some constraints known to you like maximum string length, maximum occurance of the special character ,?Sanket Makani
Most of what you need is in the Javadoc for String. That and a nested loop or two is all it takes. (Yes, this can be done easily with Streams but that's a bit advanced for this.)Mike Summers
you can consider length of string to be below 25,000 always including maximum occurrences of special character(it can have any number of occurrences below 25,000), i.e can even have a string like ,,,,,,,,, and i would replace all the special characters with every character like I mentioned in the problem.Revanth
Can you please show the result you expect for the example you gave, because I read it as: replace first "," with 'a' which will give helloa,how, then replace second "," with 'a' first and then with a,b which will give helloaaa,bhow. That just seems wrong, so please show what you expect, and maybe also what you expect for 3 or 4 commas.Andreas
hello,,how - hellowaahow , hellowabhow, hellowachow..... hellowazhow, ...... hellozahow,hellowzbhow, hellozchow......hellozzhow. If any time in this process a string satisfies a property that string will be returned and program will exit.Revanth

1 Answers

0
votes

help me out with an efficient technique

You need to keep track of what's the next replacement string:

String nextReplacement = "a";

Also keep track of the next letter:

char nextLetter = 'b';

Go through the string character-by-character. When you see a comma, insert nextReplacement, append ","+nextLetter to nextReplacement, and increment nextLetter if you have not reached z yet.

If you see a non-comma character, insert it into the output without a change.