字符串匹配-暴力匹配算法

package com.william_wei.interview.other;

/**
 * @Describe
 * @Author: william_xhw
 * @Version 1.0
 */
public class MatchStringJava {

    private Integer base = 0;

    private String str_s;

    private String str_t;

    public MatchStringJava(String str_s, String str_t) {
        this.str_s = str_s;
        this.str_t = str_t;
    }


    public Integer run() {

        int sourceLength = str_s.length();
        int targetLength = str_t.length();

        // 长度匹配
        while (base + targetLength <= sourceLength) {
            int step = 0;
            while (step < targetLength) {
                //目标串的步长位置值  与  源字符串对应位置 比较
                if (str_t.charAt(step) == str_s.charAt(base + step)) {
                    step += 1;
                    continue;
                }
                //如果不匹配,基准位置递增1
                base += 1;
                break;
            }
            //步长是否等于目标字符串的长度.
            if (step == targetLength) {
                return base;
            }
        }
        return -1;
    }

    public static void main(String[] args) {

        System.out.println(new MatchStringJava("badfsdfd", "sdf").run());

        //输出结果为4

    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值