티스토리 뷰

문제


https://leetcode.com/problems/jewels-and-stones/


You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S is a type of stone you have.  You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

Example 1:

Input: J = "aA", S = "aAAbbbb"
Output: 3

Example 2:

Input: J = "z", S = "ZZ"
Output: 0

Note:

  • S and J will consist of letters and have length at most 50.
  • The characters in J are distinct.

---------------------------------------------------------------------------------------------------------------------------------------------------


풀이


String J와 S를 input으로 받아서 J의 letter들이 S에 있는지 검사하면 되는 문제이다.


class Solution {
    public int numJewelsInStones(String J, String S) {
        
        int count = 0;
        
        for(int i=0; i < S.length(); i++){
            for(int j=0; j < J.length(); j++){
                if(J.charAt(j) == S.charAt(i)) count++;
            }    
        }
        
        return count;
        
    }
}


다음은 Submit 후의 결과창이다.


Success
Details 
Runtime: 11 ms, faster than 46.73% of Java online submissions for Jewels and Stones.
Memory Usage: 38 MB, less than 100.00% of Java online submissions for Jewels and Stones.


내 풀이의 시간복잡도는이다.


생각보다 빠르게풀었다고해서 놀랐다.


.

.

.

.

.

'알고리즘 > Leetcode' 카테고리의 다른 글

136. Single Number 풀이  (0) 2019.02.18
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함