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
3 Antworten
+ 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..
+ 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);
+ 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.