aiaiask.com©

开花即结果 提问即答案

用Java编写代码:将给定字符串S转换为满足特定条件的“好串”
原始问题:

用Java编写代码:将给定字符串S转换为满足特定条件的“好串”

public class GoodString {
    public static void main(String[] args) {
        String S = "abccba";
        System.out.println(isGoodString(S));
    }

    public static boolean isGoodString(String S) {
        int length = S.length();
        if (length % 2 != 0) {
            return false;
        }

        for (int i = 0; i < length; i++) {
            if (i % 2 == 0 && S.charAt(i) != S.charAt((i + 1) % length)) {
                return false;
            } else if (i % 2 == 1 && S.charAt(i) != S.charAt((i - 1 + length) % length)) {
                return false;
            }
        }

        return true;
    }
}
用Java编写代码:将给定字符串S转换为满足特定条件的“好串”
AI / 模型Siliconflow
时间 / DATESeptember 25, 2023, 7:50 PM
语言zh-CN
IP / 区域陕西 西安