题解 | #统计回文#

统计回文

http://www.nowcoder.com/practice/9d1559511b3849deaa71b576fa7009dc

import java.util.*;
public class Main {
    public static boolean isHw(String a) {
        char[] arr = a.toCharArray();
        int i = 0, j = arr.length - 1;
        while(i < j) {
            if(arr[i] != arr[j])
                return false;
            i++;
            j--;
        }
        return true;
    }
    public static void solve(String A, String B) {
        int cnt = 0;
        String ans = A + B;
        if(isHw(ans)) {
            cnt++;
        }
        ans = B + A;
        if(isHw(ans)) {
            cnt++;
        }
        for(int i = 1; i < A.length(); i++) {
            ans = A.substring(0, i) + B + A.substring(i, A.length());
            if(isHw(ans)) {
                cnt++;
            }
        }
        System.out.println(cnt);
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            String A = sc.next();
            String B = sc.next();
            solve(A, B);
        }
    }
}
全部评论

相关推荐

05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务