배고픈 개발자 이야기
771. Jewels and Stones (feat.PYTHON) 본문
728x90
771. Jewels and Stones
Easy
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.
풀이:
입력 문자열 J는 돌의 종류를 나타내며, S는 가지고 있는 돌을 나타냅니다.
S의 각 캐릭터는 가지고 있는 돌의 유형과 얼마나 가지고 있는지를 나타냅니다.
class Solution(object):
def numJewelsInStones(self, J, S):
J_count = 0
for x in S:
if x in J:
J_count += 1
return J_count
위와 같이 입력 배열을 역순으로 뒤집고 루프안에서 조건문을 활용하여 0과 1을 역변환 시켜줍니다.
'알고리즘 문제 > LEETCODE' 카테고리의 다른 글
[Python] MaximumDepthofBinaryTree (0) | 2021.05.06 |
---|---|
[Python] ClimbingStairs (0) | 2021.05.06 |
1009. Complement of Base 10 Integer (feat.PYTHON) (0) | 2019.09.01 |
832. Flipping an image (feat.PYTHON) (0) | 2019.09.01 |
65. Valid Number (feat. PYTHON) (0) | 2019.09.01 |
Comments