{"id":7312,"date":"2025-06-26T19:51:13","date_gmt":"2025-06-26T14:21:13","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=7312"},"modified":"2025-09-30T10:49:39","modified_gmt":"2025-09-30T05:19:39","slug":"jewels-and-stones-using-set","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/jewels-and-stones-using-set\/","title":{"rendered":"Jewels and Stones using Set"},"content":{"rendered":"\n<!-- PrismJS for Syntax Highlighting -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.min.css\" rel=\"stylesheet\">\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.min.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n<style>\n  .wp_blog_main-heading {\n    text-align: center;\n    font-size: 2.4rem;\n    color: #E58C32;\n    margin-top: 2.5rem;\n    font-weight: bold;\n  }\n\n  .wp_blog_explanation,\n  .wp_blog_code-tabs-container {\n    max-width: 940px;\n    margin: 2rem auto;\n    padding: 2rem;\n    border-radius: 12px;\n    background-color: #f9fafb;\n  }\n\n  .wp_blog_explanation h2 {\n    font-size: 1.4rem;\n    color: #E58C32;\n    margin-bottom: 0.75rem;\n  }\n\n  .wp_blog_explanation p,\n  .wp_blog_explanation li {\n    font-size: 1.05rem;\n    line-height: 1.7;\n    margin: 0.5rem 0;\n    color: #1f2937;\n  }\n\n  .wp_blog_explanation code {\n    padding: 3px 6px;\n    border-radius: 4px;\n    background: #e5e7eb;\n    font-family: 'Courier New', monospace;\n  }\n\n  .wp_blog_code-tabs-header {\n    display: flex;\n    flex-wrap: wrap;\n    gap: 0.5rem;\n    margin-bottom: 1rem;\n  }\n\n  .wp_blog_code-tab-button {\n    padding: 0.6rem 1.2rem;\n    border: 1px solid #E58C32;\n    color: #E58C32;\n    border-radius: 50px;\n    font-weight: 600;\n    cursor: pointer;\n    background-color: white;\n    transition: background 0.3s ease, color 0.3s ease;\n  }\n\n  .wp_blog_code-tab-button.active {\n    background: #E58C32;\n    color: white;\n  }\n\n  .wp_blog_code-tab-content {\n    display: none;\n    background: #111827;\n    border-radius: 12px;\n  }\n\n  .wp_blog_code-tab-content.active {\n    display: block;\n  }\n\n  .wp_blog_code-tab-content pre {\n    margin: 0;\n    padding: 1.5rem;\n    font-size: 1rem;\n    overflow-x: auto;\n    color: #f3f4f6;\n    background: #111827;\n    border-radius: 12px;\n  }\n<\/style>\n\n\n<div class=\"wp_blog_explanation\">\n  <h2>Problem Statement:<\/h2>\n  <p>You&#8217;re given strings <code>jewels<\/code> representing the types of stones that are jewels, and <code>stones<\/code> representing the stones you have. Each character in <code>stones<\/code> is a type of stone you have. You want to know how many of the stones you have are also jewels.<\/p>\n  <p>Letters are case sensitive, so <code>\"a\"<\/code> is different from <code>\"A\"<\/code>.<\/p>\n\n  <h2>Example:<\/h2>\n  <p><strong>Input:<\/strong> jewels = &#8220;aA&#8221;, stones = &#8220;aAAbbbb&#8221;<br><strong>Output:<\/strong> 3<\/p>\n  <p><strong>Input:<\/strong> jewels = &#8220;z&#8221;, stones = &#8220;ZZ&#8221;<br><strong>Output:<\/strong> 0<\/p>\n\n  <h2>Constraints:<\/h2>\n  <ul>\n    <li>1 \u2264 jewels.length, stones.length \u2264 50<\/li>\n    <li>jewels and stones consist of only English letters<\/li>\n    <li>All characters in <code>jewels<\/code> are unique<\/li>\n  <\/ul>\n\n  <h2>Approach:<\/h2>\n  <ul>\n    <li>Use a <code>Set<\/code> (or hash set) to store all characters from <code>jewels<\/code>.<\/li>\n    <li>Loop through each character in <code>stones<\/code>.<\/li>\n    <li>Increment a counter for every character found in the <code>jewels<\/code> set.<\/li>\n  <\/ul>\n\n  <h2>Time and Space Complexity:<\/h2>\n  <ul>\n    <li><strong>Time Complexity:<\/strong> O(n + m), where <code>n<\/code> is the length of <code>jewels<\/code> and <code>m<\/code> is the length of <code>stones<\/code>.<\/li>\n    <li><strong>Space Complexity:<\/strong> O(1) for storing unique characters from <code>jewels<\/code>.<\/li>\n  <\/ul>\n<\/div>\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"csharp\">C#<\/button>\n  <\/div>\n\n  <!-- JavaScript -->\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">var numJewelsInStones = function(jewels, stones) {\n    let jSet = new Set(jewels);\n    let count = 0;\n    for (let c of stones) {\n        if (jSet.has(c)) count++;\n    }\n    return count;\n};<\/code><\/pre>\n  <\/div>\n\n  <!-- C -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">int numJewelsInStones(char* jewels, char* stones) {\n    int count = 0;\n    for (int i = 0; stones[i] != '\\0'; i++) {\n        for (int j = 0; jewels[j] != '\\0'; j++) {\n            if (stones[i] == jewels[j]) {\n                count++;\n                break;\n            }\n        }\n    }\n    return count;\n}<\/code><\/pre>\n  <\/div>\n\n  <!-- C++ -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">class Solution {\npublic:\n    int numJewelsInStones(string jewels, string stones) {\n        unordered_set<char> jSet(jewels.begin(), jewels.end());\n        int count = 0;\n        for (char c : stones) {\n            if (jSet.count(c)) count++;\n        }\n        return count;\n    }\n};<\/code><\/pre>\n  <\/div>\n\n  <!-- Java -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">class Solution {\n    public int numJewelsInStones(String jewels, String stones) {\n        Set<Character> set = new HashSet<>();\n        for (char c : jewels.toCharArray()) {\n            set.add(c);\n        }\n        int count = 0;\n        for (char c : stones.toCharArray()) {\n            if (set.contains(c)) count++;\n        }\n        return count;\n    }\n}<\/code><\/pre>\n  <\/div>\n\n  <!-- Python -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">class Solution(object):\n    def numJewelsInStones(self, jewels, stones):\n        jset = set(jewels)\n        return sum(1 for c in stones if c in jset)<\/code><\/pre>\n  <\/div>\n\n  <!-- C# -->\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"csharp\">\n    <pre><code class=\"language-csharp\">public class Solution {\n    public int NumJewelsInStones(string jewels, string stones) {\n        var set = new HashSet<char>(jewels);\n        int count = 0;\n        foreach (char c in stones) {\n            if (set.Contains(c)) count++;\n        }\n        return count;\n    }\n}<\/code><\/pre>\n  <\/div>\n<\/div>\n\n<script>\n  document.addEventListener('DOMContentLoaded', () => {\n    const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n    const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n    buttons.forEach(button => {\n      button.addEventListener('click', () => {\n        const lang = button.getAttribute('data-lang');\n        buttons.forEach(btn => btn.classList.remove('active'));\n        contents.forEach(content => content.classList.remove('active'));\n        button.classList.add('active');\n        document.querySelector(`.wp_blog_code-tab-content[data-lang=\"${lang}\"]`).classList.add('active');\n      });\n    });\n  });\n<\/script>\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement: You&#8217;re given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels. Letters are case sensitive, so &#8220;a&#8221; is different from<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[260,176,811,810,174,172,173],"tags":[],"class_list":["post-7312","post","type-post","status-publish","format-standard","category-c-c-plus-plus","category-csharp","category-data-structures-and-algorithms","category-dsa","category-java","category-javascript","category-python"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=7312"}],"version-history":[{"count":2,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7312\/revisions"}],"predecessor-version":[{"id":10167,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/7312\/revisions\/10167"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=7312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=7312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=7312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}