In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to solve the problem of gems and stones in leetcode", the content is simple and easy to understand, and the organization is clear. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn this article "how to solve the problem of gems and stones in leetcode".
Title Link
https://leetcode-cn.com/problems/jewels-and-stones/
Title Description
Given the string J represents the type of gem in the stone, and the string S represents the stone you own. Each character in S represents a type of stone you own, and you want to know how many of the stones you own are gems.
The letters in J are not repeated, and all characters in J and S are letters. Letters are case-sensitive, so "a" and "A" are different types of stones.
Example 1:
Input: J = "aA", S = "aAAbbbb"
Output: 3
Example 2:
Input: J = "z", S = "ZZ"
Output: 0
Note:
S and J contain up to 50 letters.
The characters in J are not repeated.
solution train of thought
Tag: string
J is traversed first, and the characters are stored in HashSet respectively, so that they can be searched later when traversing S.
Traversing S and comparing each character with those in HashSet, if any, the result is ans++, the traversal ends, returning ans
Time complexity: O(m+n), m is the length of J, n is the length of S
code
Java version
class Solution {
public int numJewelsInStones(String J, String S) {
Set set = new HashSet();
for(int i = 0; i
< J.length(); i++) { set.add(J.charAt(i)); } int ans = 0; for(int i = 0; i < S.length(); i++) { if(set.contains(S.charAt(i))){ ans++; } } return ans; } } JavaScript版本 /** * @param {string} J * @param {string} S * @return {number} */ var numJewelsInStones = function(J, S) { const set = new Set(); for(const s of J) { set.add(s); } let ans = 0; for(const s of S) { if(set.has(s)){ ans++; } } return ans; }; 画解The above is "how to solve the problem of gems and stones in leetcode" all the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.