菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

判断str2中是否存在一个序列的集合 ix......im,使得str2(ix...im)=str1, ix不要求连续,且间隔的字符不能是str1中字符

str2="acsbassbba"

str1="abab"

不匹配

 

str2="acsbassba"

str1="abab"

匹配

 

 

对str1中出现的字符做一次hash记录.

找出所有str2中str1[0]字符的下标,for 循环此下标,如果出现不匹配,判断是不是str1的字符

 

 

public class Main
{

    public static void main(String[] args)
    {
        String str1 = "abab";
        String str2 = "acsbassbba";
        int a[] = new int[26];
        int b[] = new int[str2.length()];
        int bl = 0;
        for (int i = 0; i < str1.length(); i++)
            a[str1.charAt(0) - 'a'] = 1;
        for (int i = 0; i < str2.length(); i++)
            if (str2.charAt(i) == str1.charAt(0))
            {
                b[bl++] = i;
            }
        int si = 1;
        for (int i️ = b[0] + 1; i️ < bl; i️++)
        {
            si = 1;
            for (int j = b[0] + 1; j < str2.length() && si != str1.length(); ++j)
            {
                if (str1.charAt(si) == str2.charAt(j))
                {
                    ++si;
                }
                else
                {
                    // 判断是否是str1中的字符
                    if (a[str2.charAt(j)-'a'] == 1)
                    {
                        // 错误的匹配
                        break;
                    }
                    // 不是j++
                }
            }
        }
        if (bl != 0 && si == str1.length()) System.out.println("ok");
        else
            System.out.println("false");
    }

}

 

posted on 2018-11-09 12:13  好吧,就是菜菜  阅读(108)  评论(0编辑  收藏  举报