• 1967作为子字符串出现在单词中的字符串数目


    java解法,程度:简单

    说明

    给你一个字符串数组 patterns 和一个字符串 word ,统计 patterns 中有多少个字符串是 word子字符串。返回字符串数目。

    子字符串 是字符串中的一个连续字符序列

    示例 1:

    输入:patterns = ["a","abc","bc","d"], word = "abc"
    输出:3
    解释:
    - "a" 是 "abc" 的子字符串。
    - "abc" 是 "abc" 的子字符串。
    - "bc" 是 "abc" 的子字符串。
    - "d" 不是 "abc" 的子字符串。
    patterns 中有 3 个字符串作为子字符串出现在 word 中。
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    示例 2:

    输入:patterns = ["a","b","c"], word = "aaaaabbbbb"
    输出:2
    解释:
    - "a" 是 "aaaaabbbbb" 的子字符串。
    - "b" 是 "aaaaabbbbb" 的子字符串。
    - "c" 不是 "aaaaabbbbb" 的字符串。
    patterns 中有 2 个字符串作为子字符串出现在 word 中。
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    示例 3:

    输入:patterns = ["a","a","a"], word = "ab"
    输出:3
    解释:patterns 中的每个字符串都作为子字符串出现在 word "ab" 中。
    
    
    • 1
    • 2
    • 3
    • 4

    提示:

    • 1 <= patterns.length <= 100
    • 1 <= patterns[i].length <= 100
    • 1 <= word.length <= 100
    • patterns[i]word 由小写英文字母组成

    解题

    java中,String 类中有个contains方法

    public boolean contains(CharSequence s) {
        return indexOf(s.toString()) > -1;
    }
    
    • 1
    • 2
    • 3

    根据其中的源码可以作为解题的方法

    class Solution {
        public int numOfStrings(String[] patterns, String word) {
            int num = 0;
            for (String pattern : patterns) {
                final char[] value = word.toCharArray();
                final char[] str = pattern.toCharArray();
                final int index = indexOf(value, 0, value.length,
                        str, 0, str.length, 0);
                if (index > -1) {
                    num++;
                }
            }
            return num;
        }
    
       /**
         * String和StringBuffer共享的用于进行搜索的代码。源是要搜索的字符数组,目标是要搜索到的字符串。
         *
         * 参数:
         * source–正在搜索的字符。
         * sourceOffset–源字符串的偏移量。
         * sourceCount–源字符串的计数。
         * target–要搜索的字符。
         * targetOffset–目标字符串的偏移量。
         * targetCount–目标字符串的计数。
         * fromIndex–开始搜索的索引。
         */
    public int indexOf(char[] source, int sourceOffset, int sourceCount,
                           char[] target, int targetOffset, int targetCount,
                           int fromIndex) {
            if (fromIndex >= sourceCount) {
                return (targetCount == 0 ? sourceCount : -1);
            }
            if (fromIndex < 0) {
                fromIndex = 0;
            }
            if (targetCount == 0) {
                return fromIndex;
            }
    
            char first = target[targetOffset];
            int max = sourceOffset + (sourceCount - targetCount);
    
            for (int i = sourceOffset + fromIndex; i <= max; i++) {
                /* Look for first character. */
                if (source[i] != first) {
                    while (++i <= max && source[i] != first) ;
                }
    
                /* Found first character, now look at the rest of v2 */
                if (i <= max) {
                    int j = i + 1;
                    int end = j + targetCount - 1;
                    for (int k = targetOffset + 1; j < end && source[j]
                            == target[k]; j++, k++)
                        ;
    
                    if (j == end) {
                        /* Found whole string. */
                        return i - sourceOffset;
                    }
                }
            }
            return -1;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    String和StringBuffer共享的用于进行搜索的代码。源是要搜索的字符数组,目标是要搜索到的字符串。
    参数:
    source–正在搜索的字符。
    sourceOffset–源字符串的偏移量。
    sourceCount–源字符串的计数。
    target–要搜索的字符。
    targetOffset–目标字符串的偏移量。
    targetCount–目标字符串的计数。
    fromIndex–开始搜索的索引。

  • 相关阅读:
    淘宝API商品详情测试工具,返回数据说明
    docker安装MySQL
    多节点部署zookeeper
    几分钟实现对恶意IP地址进行拦截,腾讯云Web防火墙实在太香了!
    微信小程序——生命周期详解(代码解读)
    两个月雅思口语速成
    数据库小白入门最简易版
    ansible playbook实现磁盘格式化及文件系统挂载
    Mapbox-gl 关闭所有Popup,以及关闭按钮出现黑色边框bug
    Python 搭建 FastAPI 项目
  • 原文地址:https://blog.csdn.net/xue_mind/article/details/133810499