Algorithms practice every day 771: Find the number of string that appears

Topic Source: https://leetcode.com/problems/jewels-and-stones/
Problem Description: two strings J and S, S appeared to find out how many times a character string which contains J's.
for example:

String J String S result
aA aAAbsdfe 3
b Brbaaaa 0

solution

  1. Double traverse two strings, comprising checking whether the character substring, the time complexity of Ο (n ^ 2)
    public int numJewelsInStones(String J, String S) {
        int sum = 0;
        char[] jChar = J.toCharArray();
        char[] sChar = S.toCharArray();
        int jLength = jChar.length;
        int sLength = sChar.length;
        for(int i=0;i<jLength;i++){
            for(int j=0;j<sLength;j++){
                if(jChar[i] == sChar[j]) {
                    sum ++;
                }
            }
        }
        return sum;
    }
}

Guess you like

Origin www.cnblogs.com/xiaoyangjia/p/11692702.html