【题目描述】
给你两个字符串 a
和 b
,它们长度相同。请你选择一个下标,将两个字符串都在 相同的下标 分割开。由 a
可以得到两个字符串: aprefix
和 asuffix
,满足 a = aprefix + asuffix
,同理,由 b
可以得到两个字符串 bprefix
和 bsuffix
,满足 b = bprefix + bsuffix
。请你判断 aprefix + bsuffix
或者 bprefix + asuffix
能否构成回文串。
当你将一个字符串 s
分割成 sprefix
和 ssuffix
时, ssuffix
或者 sprefix
可以为空。比方说, s = "abc"
那么 "" + "abc"
, "a" + "bc"
, "ab" + "c"
和 "abc" + ""
都是合法分割。
如果 能构成回文字符串 ,那么请返回 true
,否则返回 false
。
注意, x + y
表示连接字符串 x
和 y
。
https://leetcode.cn/problems/split-two-strings-to-make-palindrome/
【示例】
【代码】双指针
package com.company;
// 2023-03-23
import java.util.*;
class Solution {
public boolean checkPalindromeFormation(String a, String b) {
boolean flag = true;
return check(a, b) || check(b, a);
}
public boolean check(String a, String b){
int len = a.length();
int left = 0;
int right = len - 1;
while (left right) return true;
return checkStr(a, left, right) || checkStr(b, left, right);
}
private boolean checkStr(String s, int left, int right) {
while (left = right;
}
}
public class Test {
public static void main(String[] args) {
new Solution().checkPalindromeFormation("ulacfd", "jizalu"); // 输出:true
new Solution().checkPalindromeFormation("abdef", "fecab"); // 输出:true
new Solution().checkPalindromeFormation("x", "y"); // 输出:true
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net