0

The Spy Life Java

Hello everyone! I'm having a problem with a codecoach Spy Life. The output of my code is absolutely correct (there are not any extra spaces), but it doesn't pass any test cases. Please tell me what is the problem https://code.sololearn.com/cjD6rxJn209W/?ref=app

20th May 2022, 3:21 PM
N N
3 Respuestas
+ 2
In this statement, result[in.length - 1 - i] = in[i]; In result array, you are assigning in same index location as it in 'in' array.. So both have same length with special character places are just empty. For ex: in= 'a', '@', 'b'. result = 'a', '', 'b' have empty characters between but it must less than original array length. Just be 'a', 'b' So use another variable instead of i in result[length-1-i ] like j, only increment when assigned a value..
20th May 2022, 4:04 PM
Jayakrishna 🇮🇳
+ 2
NoName Use string instead of an array and reverse for loop char[] in = input.nextLine().toCharArray(); //char[] result = new char[in.length]; String str = ""; for (int i = in.length - 1; i >= 0; i--) { if(s.indexOf(in[i]) != -1) { //result[in.length - 1 - i] = in[i]; str = str + in[i]; } } System.out.print(str);
20th May 2022, 4:28 PM
A͢J
A͢J - avatar
+ 2
Thanks a lot! Both solutions work, also I found another one. I added a for-each loop to check if character in result != Character.MIN_VALUE and if it's true, it's added to an another String called result1, and after that we get the String we need. Maybe it's not a very good practice, but it worked.
20th May 2022, 4:44 PM
N N