import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param str string字符串 待判断的字符串 * @return bool布尔型 */ public boolean judge (String str) { if (str == null || str.length() == 0 || str.length() == 1) { return true; } char[] chars = str.toCharArray(); int mid = str.length()/2; Stack<Character> stack = new Stack<>(); //abab情况 if (str.length() % 2 == 0) { for (int i = 0; i < chars.length; i++) { if (i < mid) { stack.push(chars[i]); } else { if (chars[i] != stack.pop()) { return false; } } } } else {//aba情况 for (int i = 0; i < chars.length; i++) { if (i < mid) { stack.push(chars[i]); } else if (i >= (mid + 1)) { if (chars[i] != stack.pop()) { return false; } } } } return true; } }