The problem statement of Tourist Translations boils down to the following points
You are given a mapping of the 26 english alphabets onto itself. (Let this be called map)
Transform a string using this mapping while maitaining the case.
Preserve punctuation.
Replace ‘_’ with single space.
EXPLANATION
For those new to programming, solving this problem should lead to discovery of how to map a character in the english alphabet to another. map is input in the form of a string of 26 characters, which contains a permutation of the characters.
The first character is the what ‘a’ maps to.
The second character is what ‘b’ maps to.
…
The 26th character is what ‘z’ maps to.
This can be achieved by mapping characters in english to indexes in map, and then use the character at that index. Given a string A of english alphabets, this can be done as following
for i = 1 to length(A)
A[i] = map[A[i] - 'a']
Of course, this is possible because characters in the ASCII charset are stored in a continuous block that starts at 65, which is ‘a’. The above snippet has one bug right now. We are not detecting the character case and trying to maintain it. Let us fix that.
for i = 1 to length(A)
if A[i] = lower-case-alphabet
A[i] = lower-case( map[A[i] - 'a'] )
if A[i] = upper-case-alphabet
A[i] = upper-case( map[A[i] - 'a'] )
Now, all that is needed is to handle the other two types of characters - punctuation and underscores.
for i = 1 to length(A)
if A[i] = underscore
A[i] = space
if A[i] = lower-case-alphabet
A[i] = lower-case( map[A[i] - 'a'] )
if A[i] = upper-case-alphabet
A[i] = upper-case( map[A[i] - 'a'] )
/* Ignore punctuation */
Both the Setter’s and Tester’s solution use the approach described above.
@h1t35h map[26] should be map[27] Otherwise you end up corrupting some other array (and don’t get any memory access errors… just WA because of the corruption)
It seems like you’re using you variable length to store the length of the conversation or the number of test cases but you’re assuming that only the first char contains the number of test cases and that is not true for all inputs… Since the string following the number of test cases contains only letters you should loop the firstChars array and keep adding the length value until the space character is found , and then read the string. And avoid using Scanner and System.out.println(), use BufferedReader and BufferedWriter instead, but I would recommend you to try and run your program as it is (I think for this problem Scanner and System.out.println will be fast enough since the number of test cases is at most 100). Then you can focus on your reading and writing methods. Besides BufferedReader is easier to use than Scanner, at least for me. See Why I get TLE and Java Wiki for better understanding.
the problem with my code is that after entering a sentence containing punctuation marks and a sentence with only alphabets…the output comes out containing some letters from previous output…otherwise the output will be correct…can anyone tell me what’s wrong here…thanks!!
if you have any doubts related to any of your answers and want to LEARN and SHARE good tricks and concepts in coding.join our coding community and let us all coders rock