[英]palindrome of a string in java
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.问题的约束是:第一行包含两个整数N和LN,分别表示人的数量和L,回文中人名的最大长度。 The ith of the next N lines contains a single string S denoting the name of the ith person.
接下来的 N 行中的第 i 行包含一个字符串 S,表示第 i 个人的姓名。 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这里 4 表示总人数 10 表示每个人姓名的最大长度
output is 3 as only first 3 person can change their name to its respective form..输出为 3,因为只有前 3 个人可以将他们的名字更改为其各自的形式。
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];
你应该让
String reverse[] =new String[N];
into Char reverse[] =new Char[N];
转化为
Char reverse[] =new Char[N];
Also, this reverse[i] = reverse[i] + a[i].charAt(i);
此外,这个
reverse[i] = reverse[i] + a[i].charAt(i);
should just be reverse[i] = a[i].charAt(i);
应该只是
reverse[i] = a[i].charAt(i);
I presume this is a Codechef problem.我认为这是一个 Codechef 问题。 The primary problem in your code is in the part where you assign the values to
N
and l
.代码中的主要问题在于您将值分配给
N
和l
。 Notice that you take the input in line
just once and then assign it's integer value to both N
and l
.请注意,您需要在输入
line
只是一次,然后它的整数值分配给两个N
和l
。 So, the problem is that your program is not taking an expected input : you have to take the input in line
twice .所以,问题是,你的程序没有考虑期望的输入:你必须要在输入
line
的两倍。 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;要放弃这种复杂性,请忘记第二个
reverse
数组; 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.将其传递给
StringBuilder
以将其反转。 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
.我不知道您使用
int
变量l
做什么,但是像我在上面对N
所做的那样修改它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.