题解 | 翻转01

翻转01

https://www.nowcoder.com/practice/2058328e7de94037b556fd4d892820d3

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        for(int i = 0;i < n;i++) {
            int len = in.nextInt();
            int k = in.nextInt();

            in.nextLine();

            char[] s = in.nextLine().toCharArray();

            int cnt = cntdif(s);
            if(k < cnt) {
                System.out.println("NO");    
            } else  {
                k -= cnt;
                if(len % 2 == 0) {
                    System.out.println(k % 2 == 0?"YES" : "NO");
                } else {
                    System.out.println("YES");
                }
                
            }

            

        }
    }

    public static int cntdif(char[] s) {
        int left = 0;
        int right = s.length - 1;
        int cnt = 0;
        while(left < right) {
            if(s[left] != s[right]) {
                cnt++;
            }
            left++;
            right--;
        }
        return cnt;
    }
}

全部评论

相关推荐

挥毫自在:想白嫖你呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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