OJ - LCL迴文字串
阿新 • • 發佈:2020-12-10
題目描述:
import java.util.Scanner; /** * 此類用於演示兩個字串的字串的擷取後的拼接是否為迴文 */ public class Solution { // 判斷拼接後是否能夠得到迴文字串 public static boolean getAnswer(String a, String b){ char[] achars = a.toCharArray(); char[] bchars = b.toCharArray(); int head = 0; int end = a.length()-1;int i = 0; int j = 0; while (head < end){ if (achars[head] == bchars[end]){ head++; end--; }else{ break; } } i = head; j = end; while (i<j){ if(bchars[i] == bchars[j]){ i++; j--; }else{ break; } } if (i>=j){ return true; } i=head; j=end; while (i<j){ if(achars[i] == achars[j]){ i++; j--; }else{ break; } } if (i>=j){ return true; } return false; } public static boolean isPalindrome(String a, String b){ if(getAnswer(a,b) || getAnswer(b,a)){ return true; } return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String a = sc.nextLine(); String b = sc.nextLine(); boolean result = isPalindrome(a,b); System.out.println(result); } }