题解 | 小红的回文串

小红的回文串

https://www.nowcoder.com/practice/b0c3a7aecb0a47ab865602585f4402a7



import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static final Set<Character> set1 = new HashSet<>();
    private static final Set<Character> set2 = new HashSet<>();
    static {
        set1.add('d');
        set1.add('p');
        set1.add('q');
        set1.add('b');
        set2.add('u');
        set2.add('n');
    }
    private static String s;
    private static int i ,j;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        while (t-- > 0) {
            s = in.next();
            i = 0;
            j = s.length() - 1;
            System.out.println(f() ? "YES" : "NO");
        }
    }
    private static boolean f() {
        while (i <= j && ((s.charAt(i) == s.charAt(j)) || judge())) {
            i++;
            j--;
        }
        return i >= j;
    }
    private static boolean judge() {
        if (set1.contains(s.charAt(i)) && set1.contains(s.charAt(j))) return true;
        else if (set2.contains(s.charAt(i)) && set2.contains(s.charAt(j))) return true;
        else if (set1.contains(s.charAt(i)) || set1.contains(s.charAt(j))) return false;
        else {
            StringBuffer sb = new StringBuffer();
            if (s.charAt(i) == 'w' && s.charAt(j) == 'v') {

                sb.append(s, 0, i);
                sb.append("vv");
                ++j;
                s = sb.toString() + s.substring(i + 1);
                return true;
            } else if (s.charAt(i) == 'v' && s.charAt(j) == 'w') {

                sb.append(s.substring(0, j));
                sb.append("vv");
                s = sb.toString() + s.substring(j + 1);
                ++j;

                return true;
            } else if (s.charAt(i) == 'm' && (s.charAt(j) == 'n' || s.charAt(j) == 'u')) {

                sb.append(s.substring(0, i));
                sb.append("nn");

                ++j;
                s = sb.toString() + s.substring(i + 1);
                return true;
            } else if (s.charAt(j) == 'm' && (s.charAt(i) == 'n' || s.charAt(i) == 'u')) {
                sb.append(s.substring(0, j));
                sb.append("nn");
                s = sb.toString() + s.substring(j + 1);
                ++j;

                return true;
            } else return false;
        }


    }
}
//b d q p
//w -> v v
//m -> u u n n u n

全部评论

相关推荐

牛客40297450...:不是研究生强,是你强
点赞 评论 收藏
分享
机械打工仔:很强了,直接开投就好了,求职意向不用专门写出来,你给哪个岗投简历你的求职意向就是哪个
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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