package CodeChef;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CodeChefRAINBOWA{
public static void main(String[] args) throws java.lang.Exception {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
try {
int t = Integer.parseInt(sc.readLine());
while(t-->0) {
int num = Integer.parseInt(sc.readLine());
String str = sc.readLine();
int start = str.indexOf('7');
int last = str.lastIndexOf('7');
String subString1= str.substring(0,start-1);
String subString2 = str.substring(last+1);
StringBuilder input1 = new StringBuilder();
input1.append(subString2);
input1= input1.reverse();
String compare = input1.toString();
// System.out.println(subString1.toString().equals(compare)); ignore this
// ISSSUE IS HERE! always giving false
if(subString1.equals(compare)) {
System.out.println("yes");
}
else
System.out.println("no");
}
} catch (Exception e) {
}
}
}
ssjgz
October 26, 2019, 4:38pm
2
Please either format your code or link to your submission
Can you give a testcase that shows the issue you’re having?
Edit:
Your code will throw an Exception (which is silently smothered) if the input does not contain a 7
.
Edit2:
Given the testcase
1
13
1 2 3 4 5 6 7 6 5 4 3 2 1
(which should output yes
), subString1
and compare
are not the same string - can you see why?
1 Like
I havent submitted the code yet …this is the problem code RAINBOWA
package CodeChef;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CodeChefRAINBOWA{
public static void main(String[] args) throws java.lang.Exception {
BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
try {
int t = Integer.parseInt(sc.readLine());
while(t-->0) {
int num = Integer.parseInt(sc.readLine());
String str = sc.readLine();
int start = str.indexOf('7');
int last = str.lastIndexOf('7');
String subString1= str.substring(0,start-1);
String subString2 = str.substring(last+1);
StringBuilder input1 = new StringBuilder();
input1.append(subString2);
input1= input1.reverse();
String compare = input1.toString();
// System.out.println(subString1.toString().equals(compare)); ignore this
// ISSSUE IS HERE! always giving false
if(subString1.equals(compare)) {
System.out.println("yes");
}
else
System.out.println("no");
}
} catch (Exception e) {
}
}
}
1 Like
ssjgz
October 26, 2019, 4:58pm
5
Thanks - there are some hints as to what is going wrong in my first post, above
I am not getting it plz explain … …sorry fr the trouble
thank you
ssjgz
October 26, 2019, 5:05pm
7
Use the testcase I provided. Print out the values of subString1
and compare
using e.g.
System.out.println("substring: >" + subString1 + "< compare: >" + compare + "<");
Observe that they are not the same, so that subString1.equals(compare)
is false
, and no
is (incorrectly) output.
1 Like
i have tried debugging the code in eclipse …the problem is equals in always giving FALSE …inspite the fact that both the strings are having the same values.
13
1 2 3 4 5 6 7 6 5 4 3 2 1
substring: >1 2 3 4 5 6< compare: >1 2 3 4 5 6 <
no
This is the eclipse output
ssjgz
October 26, 2019, 5:12pm
11
Yep - so - are subString1
and compare
the same string, and so what should subString1.equals(compare)
return - true
or false
?
its always giving FALSE i have tried printing the return boolean
ssjgz
October 26, 2019, 5:16pm
13
It’s giving false
because the strings you’re comparing aren’t equal (look at the output you posted above very carefully) Your task is to debug why.
ssjgz
October 26, 2019, 5:20pm
15
Yes! So subString1
and compare
can (probably?) never be equal, because compare
will always have an extra space. Now you have to find out how to prevent this.
1 Like
you are awesome…!!!
i could have never figured it out myself thank you so much.
I will try to fix it . :blush
1 Like
ssjgz
November 2, 2019, 8:04am
18
No, you can’t ask about questions from ongoing contests!!
Please delete your post.
2 Likes