The constraint of the question is that: The first line contains two integers N and LN, denoting the number of person and L,the maximum length of a person's name in Palindrome respectively. The ith of the next N lines contains a single string S denoting the name of the ith person. They can do that by choosing at most two non-overlapping substrings of their own name and reversing them.
My code is compiled properly but is giving run time error: So what needs to be changed? here is my code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
class Palindrome {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
{
String line = br.readLine();
int N = Integer.parseInt(line);
int l = Integer.parseInt(line);
String a[] = new String[N];
int c = 0;
for (int i = 0; i < N; i++) {
System.out.println("Enter names");
a[i] = br.readLine();
}
String reverse[] = new String[N];
int arrayLength = a.length;
for (int i = arrayLength - 1; i >= 0; i--) {
reverse[i] = reverse[i] + a[i].charAt(i);
}
if (Arrays.equals(a, reverse)) //if (a[i].equals(reverse[i]))
{
c++;
System.out.println(c);
}
}
}
}
I need to print the number of persons that can change their name to Palindrome form.
Example:
Input:
4 10
aacbaac
acbdabc
abcdcba
abcbd
and
output:
3
here 4 denotes total number of person 10 denotes the maximum length of each person name
output is 3 as only first 3 person can change their name to its respective form..
One issue you have is that you defined a string array, but you're trying to pass characters into it. You should make String reverse[] =new String[N];
into Char reverse[] =new Char[N];
Also, this reverse[i] = reverse[i] + a[i].charAt(i);
should just be reverse[i] = a[i].charAt(i);
I presume this is a Codechef problem. The primary problem in your code is in the part where you assign the values to N
and l
. Notice that you take the input in line
just once and then assign it's integer value to both N
and l
. So, the problem is that your program is not taking an expected input : you have to take the input in line
twice . Change that part to
String line = br.readLine();
int N = Integer.parseInt(line);
line = br.readLine(); //add this line before assigning l's value
int l= Integer.parseInt(line);
Besides, what you are trying to do in the loop while finding the reverse of the string should be done in a nested loop construct. To abandon that complication, forget the second reverse
array; you can use this one line statement to check for a palindrome string:
for(int i=0; i<a.length; i++){
if(String.valueOf(new StringBuffer(a[i]).reverse()).equalsIgnoreCase(a[i]))
//a[i] is a palindrome, do whatever you want to
}
Pass it to a StringBuilder
inorder to reverse it. Review the code below:
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int N = Integer.parseInt(line);
int l = Integer.parseInt(line);
String a[] = new String[N];
int c = 0;
for (int i = 0; i < N; i++) {
System.out.println("Enter names");
a[i] = br.readLine();
}
String reverse[] = new String[N];
int arrayLength = a.length;
for (int i = arrayLength - 1; i >= 0; i--) {
StringBuilder build = new StringBuilder(a[i]);
reverse[i] = build.reverse().toString();
if (a[i].equals(reverse[i])) {
c++;
}
}
System.out.println("Pal is " + c);
}
I don't know what you are using the int
variable l
for, but modify it like I have done above with the N
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.