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 中。

示例 2:

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

示例 3:

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

提示:

  • 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;
}

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

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;
    }
}

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

你可能感兴趣的:(算法习题,算法,java)