<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: jihyun-j</title>
    <description>The latest articles on Forem by jihyun-j (@jihyunj).</description>
    <link>https://forem.com/jihyunj</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F721454%2F20e0044f-ff87-4615-9967-538704e7c535.JPG</url>
      <title>Forem: jihyun-j</title>
      <link>https://forem.com/jihyunj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jihyunj"/>
    <language>en</language>
    <item>
      <title>[Algorithm] 151. Reverse Words in a String</title>
      <dc:creator>jihyun-j</dc:creator>
      <pubDate>Wed, 05 Feb 2025 02:58:13 +0000</pubDate>
      <link>https://forem.com/jihyunj/algorithm-151-reverse-words-in-a-string-5com</link>
      <guid>https://forem.com/jihyunj/algorithm-151-reverse-words-in-a-string-5com</guid>
      <description>&lt;h2&gt;
  
  
  Description
&lt;/h2&gt;

&lt;p&gt;Given an input string &lt;code&gt;s&lt;/code&gt;, reverse the order of the &lt;strong&gt;words&lt;/strong&gt;.&lt;br&gt;
A &lt;strong&gt;word&lt;/strong&gt; is defined as a sequence of non-space characters. The &lt;strong&gt;words&lt;/strong&gt; in s will be separated by at least one space.&lt;br&gt;
Return &lt;em&gt;a string of the words in reverse order concatenated by a single space&lt;/em&gt;.&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt; that &lt;code&gt;s&lt;/code&gt; may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Remove extra spaces between words and at the beginning/end of the string.&lt;/li&gt;
&lt;li&gt;Reverse the order of words (not characters).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 1:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: s = "the sky is blue"
Output: "blue is sky the"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 2:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: s = "  hello world  "
Output: "world hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 3:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: s = "a good   example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✔️ My Submission
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;reverseWords&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\s&lt;/span&gt;&lt;span class="sr"&gt;+/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Removed all extra spaces from &lt;code&gt;s&lt;/code&gt; using the regular expression &lt;code&gt;/\s+/g&lt;/code&gt; with the &lt;code&gt;replace&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;However, this alone did not remove the leading and trailing spaces, so I added the &lt;code&gt;trim&lt;/code&gt; method to eliminate them.&lt;/li&gt;
&lt;li&gt;Next, I used &lt;code&gt;split(' ')&lt;/code&gt; to break the string into an array of words.&lt;/li&gt;
&lt;li&gt;Since it’s now an array, I applied the reverse method to rearrange the words in &lt;code&gt;reverse&lt;/code&gt; order.&lt;/li&gt;
&lt;li&gt;Finally, I used the &lt;code&gt;join(' ')&lt;/code&gt; method to convert the array back into a string.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>leetcode</category>
      <category>frontend</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>[Algorithm] 1431. Kids With the Greatest Number of Candies</title>
      <dc:creator>jihyun-j</dc:creator>
      <pubDate>Fri, 31 Jan 2025 14:22:48 +0000</pubDate>
      <link>https://forem.com/jihyunj/algorithm-1431-kids-with-the-greatest-number-of-candies-i2p</link>
      <guid>https://forem.com/jihyunj/algorithm-1431-kids-with-the-greatest-number-of-candies-i2p</guid>
      <description>&lt;h2&gt;
  
  
  Description
&lt;/h2&gt;

&lt;p&gt;There are &lt;code&gt;n&lt;/code&gt; kids with candies. You are given an integer array &lt;code&gt;candies&lt;/code&gt;, where each &lt;code&gt;candies[i]&lt;/code&gt; represents the number of candies the &lt;code&gt;ith&lt;/code&gt; kid has, and an integer &lt;code&gt;extraCandies&lt;/code&gt;, denoting the number of extra candies that you have.&lt;/p&gt;

&lt;p&gt;Return a boolean array &lt;code&gt;result&lt;/code&gt; of length &lt;code&gt;n&lt;/code&gt;, where &lt;code&gt;result[i]&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; if, after giving the &lt;code&gt;ith&lt;/code&gt; kid all the &lt;code&gt;extraCandies&lt;/code&gt;, they will have the greatest number of candies among all the kids, or &lt;code&gt;false&lt;/code&gt; otherwise.&lt;/p&gt;

&lt;p&gt;Note that multiple kids can have the greatest number of candies.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⭐️ Key Requirement
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Each kid can receive all &lt;code&gt;extraCandies&lt;/code&gt; at once, meaning we check each kid individually.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Compare with the maximum number of candies to see if the kid can have the most after receiving &lt;code&gt;extraCandies&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return a boolean array, where &lt;code&gt;true&lt;/code&gt; means the kid has the greatest number of candies, and &lt;code&gt;false&lt;/code&gt; means they do not.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example 1:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true] 
Explanation: If you give all extraCandies to:
- Kid 1, they will have 2 + 3 = 5 candies, which is the greatest among the kids.
- Kid 2, they will have 3 + 3 = 6 candies, which is the greatest among the kids.
- Kid 3, they will have 5 + 3 = 8 candies, which is the greatest among the kids.
- Kid 4, they will have 1 + 3 = 4 candies, which is not the greatest among the kids.
- Kid 5, they will have 3 + 3 = 6 candies, which is the greatest among the kids.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 2:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false] 
Explanation: There is only 1 extra candy.
Kid 1 will always have the greatest number of candies, even if a different kid is given the extra candy.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 3:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Constraints:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;n == candies.length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;2 &amp;lt;= n &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= candies[i] &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= extraCandies &amp;lt;= 50&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  📌 My Submission
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @param {number[]} candies
 * @param {number} extraCandies
 * @return {boolean[]}
 */&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;kidsWithCandies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;candies&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;extraCandies&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;candies&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;candies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;candies&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;extraCandies&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create an empty array to store the results.&lt;/li&gt;
&lt;li&gt;Find the maximum number in the &lt;code&gt;candies&lt;/code&gt; array to use as a reference for comparison.&lt;/li&gt;
&lt;li&gt;Loop through the &lt;code&gt;candies&lt;/code&gt; array and check if &lt;code&gt;candies[i] + extraCandies&lt;/code&gt; is greater than or equal to the maximum value.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;candies[i] + extraCandies&lt;/code&gt; is greater than or equal to the maximum, push &lt;code&gt;true&lt;/code&gt; to the result array; otherwise, push &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Return the result array.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>frontend</category>
    </item>
    <item>
      <title>[Algorithm] 771. Jewels and Stones</title>
      <dc:creator>jihyun-j</dc:creator>
      <pubDate>Mon, 27 Jan 2025 06:37:36 +0000</pubDate>
      <link>https://forem.com/jihyunj/algorithm-771-jewels-and-stones-5661</link>
      <guid>https://forem.com/jihyunj/algorithm-771-jewels-and-stones-5661</guid>
      <description>&lt;h2&gt;
  
  
  Description
&lt;/h2&gt;

&lt;p&gt;You’re given strings &lt;code&gt;jewels&lt;/code&gt; representing the types of stones that are &lt;code&gt;jewels&lt;/code&gt;, and &lt;code&gt;stones&lt;/code&gt; representing the &lt;code&gt;stones&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;Letters are case sensitive, so &lt;code&gt;"a"&lt;/code&gt; is considered a different type of stone from &lt;code&gt;"A"&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Requirements of the Problem
&lt;/h2&gt;

&lt;p&gt;Find the characters in the jewels string that are present in the stones string and return the total count of those characters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example 1
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: jewels = "aA", stones = "aAAbbbb"
Output: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: jewels = "z", stones = "ZZ"
Output: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Constraints:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;1 &amp;lt;= jewels.length, stones.length &amp;lt;= 50&lt;/li&gt;
&lt;li&gt;jewels and stones consist of only English letter.&lt;/li&gt;
&lt;li&gt;All the characters of jewels are unique.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  My Submission 1 — Using the &lt;code&gt;includes()&lt;/code&gt; Method
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @param {string} jewels
 * @param {string} stones
 * @return {number}
 */&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;numJewelsInStones&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jewels&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;stones&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;stones&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jewels&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stones&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;])){&lt;/span&gt;
            &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Initialize the &lt;code&gt;count&lt;/code&gt; variable to store the final count of jewels.&lt;/li&gt;
&lt;li&gt;Loop through the &lt;code&gt;stones&lt;/code&gt; string, and check if each character is present in the &lt;code&gt;jewels&lt;/code&gt; string using the &lt;code&gt;includes()&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;If it is included, increment the &lt;code&gt;count&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;Finally, return the &lt;code&gt;count&lt;/code&gt; variable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This solution has a time complexity of &lt;code&gt;O(m×n)&lt;/code&gt;, where &lt;code&gt;m&lt;/code&gt; is the length of the stones string and &lt;code&gt;n&lt;/code&gt; is the length of the &lt;code&gt;jewels&lt;/code&gt; string. This is because the &lt;code&gt;includes()&lt;/code&gt; method iterates through the &lt;code&gt;jewels&lt;/code&gt; string for each character in &lt;code&gt;stones&lt;/code&gt;. If the input size increases, this approach becomes inefficient. We need to optimize the solution to reduce the time complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Submission 2 — Using the &lt;code&gt;Set()&lt;/code&gt; Method (Hash Table)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/**
 * @param {string} jewels
 * @param {string} stones
 * @return {number}
 */&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;numJewelsInStones&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jewels&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;stones&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jewelsMap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jewels&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;stones&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jewelsMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;has&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stones&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Initialize the &lt;code&gt;count&lt;/code&gt; variable to store the final count of jewels.&lt;/li&gt;
&lt;li&gt;Create a &lt;code&gt;Set&lt;/code&gt; from the &lt;code&gt;jewels&lt;/code&gt; string. A &lt;code&gt;Set&lt;/code&gt; allows for faster lookups because it uses a hash table internally.&lt;/li&gt;
&lt;li&gt;Loop through the &lt;code&gt;stones&lt;/code&gt; string, and check if each character exists in the &lt;code&gt;Set&lt;/code&gt; using the &lt;code&gt;has()&lt;/code&gt; method.&lt;/li&gt;
&lt;li&gt;If the character exists, increment the &lt;code&gt;count&lt;/code&gt; variable.&lt;/li&gt;
&lt;li&gt;Finally, return the &lt;code&gt;count&lt;/code&gt; variable.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;Set()&lt;/code&gt; is Faster Than &lt;code&gt;includes()&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;includes()&lt;/code&gt; method checks for a value by iterating through the &lt;code&gt;jewels&lt;/code&gt; string one character at a time. This means the time complexity for each check is &lt;code&gt;O(n)&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the length of the &lt;code&gt;jewels&lt;/code&gt; string.&lt;/p&gt;

&lt;p&gt;On the other hand, the &lt;code&gt;Set&lt;/code&gt; data structure uses a hash table internally. This allows for constant-time &lt;code&gt;O(1)&lt;/code&gt; lookups using the &lt;code&gt;has()&lt;/code&gt; method. While creating the &lt;code&gt;Set&lt;/code&gt; initially takes &lt;code&gt;O(n)&lt;/code&gt; time, subsequent lookups are much faster compared to the &lt;code&gt;includes()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;-&lt;/p&gt;

&lt;p&gt;If you find any mistakes in my post or have a different perspective, feel free to leave a comment below. I always enjoy learning from different viewpoints! 😊 Also, if you liked this post, I'm happy to connect on &lt;a href="https://www.linkedin.com/in/jihyunjeon/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; anytime.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>frontend</category>
      <category>algorithms</category>
      <category>leetcode</category>
    </item>
  </channel>
</rss>
