对于一个字符串,我们想通过添加字符的方式使得新的字符串整体变成回文串,但是只能在原串的结尾添加字符,请返回在结尾添加的最短字符串。
给定原字符串A及它的长度n,请返回添加的字符串。保证原串不是回文串。
测试样例:
"ab",2
返回:"a"
public String addToPalindrome(String A, int n) { // write code here if(A==null||n<=0) { return null; } int center=(n-1)/2+1; int deviation=1; int greatestLength=0; //从中间向后一个字符开始查找 判断是否有回文串 //查找奇数回文串 while(center<n) { deviation=1; greatestLength=0; while(center+deviation<n&&A.charAt(center+deviation)==A.charAt(center-deviation)) { deviation++; greatestLength++; } if(center+deviation==n)//前面的字符中有回文串 且长度为deviation { break; } center++; } int deviationEven=1;//偶数回文串偏移量 int greatestLengthEven=0; int oddCenter=center; center=(n-1)/2+1; //查找偶数回文串 while(center<n-1) { deviationEven=1; greatestLengthEven=0; if(A.charAt(center)==A.charAt(center+1)) { while(center+1+deviationEven<n&&A.charAt(center+deviationEven+1)==A.charAt(center-deviationEven)) { deviationEven++; greatestLengthEven++; } if(center+1+deviationEven==n) { break; } } center++; } StringBuffer str=new StringBuffer(); if(greatestLength>greatestLengthEven) { for(int i=oddCenter-deviation;i>=0;i--) { str.append(A.charAt(i)); } } else { for(int i=center-deviationEven;i>=0;i--) { str.append(A.charAt(i)); } } return str.toString(); }
public static boolean isPalindrome(String s){ StringBuffer stringBuffer = new StringBuffer(s); StringBuffer reverse_ = stringBuffer.reverse(); String reverse = reverse_.toString(); if (s.equals(reverse)) { return true; } return false; } public static String addToPalindrome(String A, int n){ StringBuffer add = new StringBuffer(); for(int i=0; i<n; i++){ if (isPalindrome(A.substring(i,n))) { StringBuffer reverse = add.reverse(); return reverse.toString(); } add.append(A.charAt(i)); } return null; }
import java.util.*; public class Palindrome { /** * 如果字符串不回文 尾部加上str[0] 看str[1]到原字符串结尾是否回文 * 不会文 尾部加上str[1] 看str[2]到原字符串结尾是否回文 * ...... * 直到数组回文 * 得到添加的字符串 反转就是结果 */ public String addToPalindrome(String A, int n) { char[] ch = A.toCharArray(); StringBuilder sb = new StringBuilder(); int start = 0; while (!isPalindrome(ch,start,ch.length-1)){ sb.append(ch[start]); start++; } sb.reverse(); return sb.toString(); } private boolean isPalindrome(char[] ch,int start,int end){ while (start < end){ if(ch[start++] != ch[end--]) return false; } return true; } }
//leetcode KMP import java.util.*; public class Palindrome { public static String addToPalindrome(String A, int n) { String rev_A=new StringBuffer(A).reverse().toString(); String union=rev_A+"#"+A; int[] p=new int[2*n+1]; int j=0; for(int i=1;i<2*n+1;i++){ while(j>0&&union.charAt(j)!=union.charAt(i)) j=p[j-1]; if(union.charAt(j)==union.charAt(i)) j++; p[i]=j; } return rev_A.substring(p[2*n], n); } }
import java.util.*; public class Palindrome { public String addToPalindrome(String A, int n) { // write code here char[] cs = A.toCharArray(); int i = 0; int j = n - 1; int index = -1; while (i <= j) { if (cs[i] == cs[j]) { ++i; --j; } else { if (j == n - 1) ++i; j = n - 1; index = i; } } if (index == -1) return ""; else { return new StringBuilder(A.substring(0, index)).reverse().toString(); } } }
import java.util.*; // 在尾部添加形成回文串,等同于在头部删除形成回文串.添加的字符串等同于删除的字符串的倒序 public class Palindrome { public String addToPalindrome(String A, int n) { Stack<Character> stack = new Stack<Character>(); for (int i = 0; i < A.length(); i ++ ) { stack.push(A.charAt(i)); if(check(A.substring(i + 1))) break; } String res = ""; while ( ! stack.isEmpty()) res += stack.pop() + ""; return res; } public static boolean check(String s) { if(s.length() <= 1) return true; for (int i = 0; i < s.length() / 2; i ++ ) if(s.charAt(i) != s.charAt(s.length() - 1 - i)) return false; return true; } }
public String addToPalindrome(String A, int n) {
char[] a = A.toCharArray(); int answer = 0; String str = ""; boolean flag = true; for(int i = 0;i < a.length - 1;i++){ flag = true; if(a[a.length - 1] != a[i]){ answer ++; }else{ for(int j = 1;j < a.length - i;j++){ if(a[a.length - j - 1] != a[i + j]){ answer++; flag = false; break; } } if(flag == true){ break; } } } for(int x = answer - 1;x >= 0;x--){ str += a[x]; } return str; }