I am given a string S
and I have to make it a palindrome by re-arranging the characters of the given string.
If the given string can not be converted into a palindrome by re-arranging characters, print false and if it is possible to make it a palindrome, print true
My code:
String a=new String(br.readLine()); //given string
int n= a.length();
int j=0,k=n-1,count=0;
boolean flag=false;
for(int i=0;i<n;i++)
{
if(a.charAt(i)=='*')
continue; //for skipping already shifted chars
int ix = a.indexOf(a.charAt(i), i+1);
if(ix >= 0)
{ a=a.substring(0,i+1)+a.substring(i+1, ix) + "*" + a.substring(ix+1);
}
else
{
count++; //number of unique chars which can only be 1 or 0
if(count<=1 && n%2==1)
{
a=a.replaceFirst(a.substring(i,i+1),"*"); //giving middle position to the only unique char at center and replacing it with *
}
else
{
System.out.println("false"); //if more than one unique char, palindrome not possible
flag=true; // shows not possible
break;
}
}
}
if(!flag) // if possible
{
System.out.println("true");
}
答案 0 :(得分:1)
一个明显的优化将取代:
if(a.substring(i+1).contains(a.substring(i,i+1)))
{
ans[j++]=i; //storing new positions in ans array
ans[k--]=a.substring(i+1).indexOf(a.charAt(i))+1+i;
a=a.substring(0,i+1)+a.substring(i+1).replaceFirst(a.substring(i,i+1),"*"); //replacing the shifted char with *
}
使用:
int ix = a.indexOf(a.charAt(i), i+1);
if(ix >= 0)
{
ans[j++]=i; //storing new positions in ans array
ans[k--]=ix;
a=a.substring(0,i+1)+a.substring(i+1, ix) + "*" + a.substring(ix+1);
}
更新
我想知道以下代码是否会更快。数组上没有indexOf,所以我不得不做一个循环,但是没有字符串操作:
char[] c = a.toCharArray();
int n= c.length;
int ans[]=new int[n]; // for storing new positions after shifting
int j=0,k=n-1,count=0;
boolean flag=false;
for(int i=0; i < n; i++)
{
char ch = c[i];
if(ch=='*')
continue; //for skipping already shifted chars
int ix = i;
do {
++ix;
} while (ix < n && c[ix] != ch);
if(ix < n)
{
ans[j++]=i;
ans[k--]=ix;
c[ix] = '*';
}
else
{
count++; //number of unique chars which can only be 1 or 0
if(count<=1 && n%2==1)
{
ans[(int)n/2]=i;
c[i] = '*';
}
else
{
System.out.println("-1"); //if more than one unique char, palindrome not possible
flag=true; // shows not possible
break;
}
}
}
更新2
当j到达n / 2时,您也可以停止:
char[] c = a.toCharArray();
int n= c.length;
int ans[]=new int[n]; // for storing new positions after shifting
int j=0,k=n-1,count=0,half=n/2;
boolean flag=false;
for(int i=0; i < n; i++)
{
char ch = c[i];
if(ch=='*')
continue; //for skipping already shifted chars
int ix = i;
do {
++ix;
} while (ix < n && c[ix] != ch);
if(ix < n)
{
ans[j++]=i;
ans[k--]=ix;
c[ix] = '*';
if (j > half) {
break;
}
}
else
{
count++; //number of unique chars which can only be 1 or 0
if(count<=1 && n%2==1)
{
ans[half]=i;
c[i] = '*';
}
else
{
System.out.println("-1"); //if more than one unique char, palindrome not possible
flag=true; // shows not possible
break;
}
}
}