java中如何查询一个指定的子字符在字符串中出现的所有or指定次数的下标索引

在这里插入图片描述

前言

最近刷到一道很常见的算法题,题目不难,实现方式很多,就看时间复杂度哪种最低了。

话不多说,看题:


【问】 已知字符串str1=“ABABCDBADBCDBABCDBADBBADBBCCDBABCDBADBBCCDBADBBCCDBABCD”, str2=“ABCD”,如何快速高效的找出str2在str1中出现的所有下标索引以及第三次出现的下标索引?

博主一看:哇喔,这不是so easy 嘛??


开始操作。

1、indexof

	  public static void main(String[] args) {

        String str1 = "ABABCDBADBCDBABCDBADBBADBBCCDBABCDBADBBCCDBADBBCCDBABCD";
        String str2 = "ABCD";

        System.out.println(getIndexs(str1, str2));
        System.out.println(getIndex(str1, str2));

    }

    /**
     * 每找到一个“ABCD”,就将字符串截取,并将每次截取位置+下标累加即可
     *
     * @param str1
     * @param str2
     * @return
     */
    static List<Integer> getIndexs(String str1, String str2) {

        int length = str2.length();

        int index;
        int location = 0;
        List<Integer> indexs = new ArrayList<>();
        while (-1 != str1.indexOf(str2)) {
            index = str1.indexOf(str2);

            indexs.add(location + index);

            index = index + length;

            location = location + index;

            str1 = str1.substring(index);

        }
        return indexs;
    }

    /**
     * 每找到一个“ABCD”,就将字符串截取,直到找到第三个,将每次下标累加即可。
     *
     * @param str1
     * @param str2
     * @return
     */
    static Integer getIndex(String str1, String str2) {
        int num = 0;
        int location = 0;

        int index = str1.indexOf(str2);
        while (-1 != str1.indexOf(str2)) {
            location += index;

            num++;
            if (3 == num) {
                break;
            } else {
                str1 = str1.substring(index);
                index = str1.indexOf(str2, 1);
            }
        }

        return location;
    }

2、正则

	  public static void main(String[] args) {

        String str1 = "ABABCDBADBCDBABCDBADBBADBBCCDBABCDBADBBCCDBADBBCCDBABCD";
        String str2 = "ABCD";

        System.out.println(getIndexs(str1, str2));
        System.out.println(getIndex(str1, str2));

    }

    static List<Integer> getIndexs(String str1, String str2) {
        List<Integer> indexs = new ArrayList<>();
      
        int index;
        Pattern p = Pattern.compile(str2);
        Matcher m = p.matcher(str1);

        while (m.find()) {
            index=m.start();
            indexs.add(index);
        }

        return indexs;
    }

    
    static Integer getIndex(String str1, String str2) {
  	    int index = 0;
        int num = 0;
        Pattern p = Pattern.compile(str2);
        Matcher m = p.matcher(str1);

        while (m.find()) {
            num++;
            if (3 == num) {
                index=m.start();
            }
        }

        return index;
    }

3、split

 public static void main(String[] args) {

        String str1 = "ABABCDBADBCDBABCDBADBBADBBCCDBABCDBADBBCCDBADBBCCDBABCD";
        String str2 = "ABCD";

        System.out.println(getIndexs(str1, str2));
        System.out.println(getIndex(str1, str2));

    }

    static List<Integer> getIndexs(String str1, String str2) {
        int length = str2.length();
        String[] split = str1.split(str2);

        int index = 0;

        List<Integer> indexs = new ArrayList<>();
        for (String s : split) {

            indexs.add(index + s.length());
            index = index + s.length() + length;
        }

        return indexs;
    }

    static Integer getIndex(String str1, String str2) {
        int length = str2.length();
        String[] split = str1.split(str2);

        int index = 0;
        int num = 0;
        for (String s : split) {
			num++;
			
            index = index + s.length();

            if (num == 3) {
                break;
            }
            index = index + length;
        }

        return index;
    }

4、计算hash

public static void main(String[] args) {

        String str1 = "ABABCDBADBCDBABCDBADBBADBBCCDBABCDBADBBCCDBADBBCCDBABCD";
        String str2 = "ABCD";

        System.out.println(getIndexs(str1, str2));
        System.out.println(getIndex(str1, str2));

    }

    static List<Integer> getIndexs(String str1, String str2) {
        int length = str2.length();
        int length1 = str1.length();
        int i = str2.hashCode();

        List<Integer> indexs = new ArrayList<>();
        
        for (int i2 = 0; i2 < length1 - length + 1; i2++) {
            String substring = str1.substring(i2, i2 + length);
            int i1 = substring.hashCode();
            
            if (i == i1) {
                indexs.add(i2);
            }
        }

        return indexs;
    }

    static Integer getIndex(String str1, String str2) {

        int length = str2.length();
        int length1 = str1.length();
        int i = str2.hashCode();
        
        int index = 0;
        int num = 0;
        
        for (int i2 = 0; i2 < length1 - length + 1; i2++) {
            String substring = str1.substring(i2, i2 + length);
            int i1 = substring.hashCode();
            
            if (i == i1) {
                num++;
                
                if (num == 3) {
                    index = i2;
                    break;
                }
                
            }
        }

        return index;
    }

这几种方式的运行结果:

在这里插入图片描述
完结!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Romeo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值