面试题1:字符串校验 将错误的字符通过校验修改后使其成为正确的字符

今天刷到一个笔试题要求如下:

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32M,其他语言64M

题目内容:
1. 三个同样的字母连在一起,一定是拼写错误,去掉一个的就好啦:比如 helllo -> hello
2. 两对一样的字母(AABB型)连在一起,一定是拼写错误,去掉第二对的一个字母就好啦:比如 helloo -> hello
3. 上面的规则优先“从左到右”匹配,即如果是AABBCC,虽然AABB和BBCC都是错误拼写,应该优先考虑修复AABB,结果为AABCC

输入描述:
第一行包括一个数字N,表示本次用例包括多少个待校验的字符串。
后面跟随N行,每行为一个待校验的字符串。

输出描述:
N行,每行包括一个被修复后的字符串。
输入例子1:
2
helloo
wooooooow

输出例子1:
hello
woow

第一种比较复杂的方法:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void question() {
        Scanner sc = new Scanner(System.in);
        // 待校验的字符串个数
        int N = sc.nextInt();
        // 存储输入的待校验字符串
        List<String> strList = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            strList.add(sc.next());
        }
        // 存储第一次校验的结果
        List<String> firstResult = new ArrayList<>();
        // 先校验三个同样的字母连在一起的情况
        for (String s : strList) {
            boolean flag = true;
            while (flag) {
                boolean flag2 = false;
                for (int i = 0; i < s.length() - 2; i++) {
                    // 判断临近三个字符是否相同
                    if (s.charAt(i) == s.charAt(i + 1) && s.charAt(i + 1) == s.charAt(i + 2)) {
                        s = s.substring(0, i) + s.substring(i + 1);
                        flag2 = true;
                    }
                }
                if (!flag2) {
                    // 终止循环
                    flag = false;
                }
            }
            firstResult.add(s);
        }
        // 校验两对一样的字母(AABB型)连在一起的情况
        strList.clear();
        for (String s : firstResult) {
            boolean flag = true;
            while (flag) {
                boolean flag2 = false;
                for (int i = 0; i < s.length() - 3; i++) {
                    // 判断相邻四个字符是否符合AABB型
                    if (s.charAt(i) == s.charAt(i + 1) && s.charAt(i + 2) == s.charAt(i + 3)) {
                        s = s.substring(0, i + 2) + s.substring(i + 3);
                        flag2 = true;
                    }
                }
                if (!flag2) {
                    flag = false;
                }
            }
            strList.add(s);
        }
        // 输出结果
        for (String s : strList) {
            System.out.println(s);
        }
    }
}

第二种使用正则表达式:

public class Main {
   public static void question() {
        Scanner sc = new Scanner(System.in);
        // 待分析的字符串个数
        int N = sc.nextInt();
        // 存储结果
        List<String> result = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            result.add(sc.next().replaceAll("(.)\\1+", "$1$1").replaceAll("(.)\\1(.)\\2", "$1$1$2"));
        }
        // 输出结果
        for (String s : result) {
            System.out.println(s);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值