<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chan Park on Medium]]></title>
        <description><![CDATA[Stories by Chan Park on Medium]]></description>
        <link>https://medium.com/@thechanmoon?source=rss-3bedef55bc1d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*h0HVdqPF315lpcmWzJMCFw.jpeg</url>
            <title>Stories by Chan Park on Medium</title>
            <link>https://medium.com/@thechanmoon?source=rss-3bedef55bc1d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 22:25:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@thechanmoon/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Leetcode 125 Valid Palindrome ( C,C++,C#,Java,Javascript,Ruby)]]></title>
            <link>https://thechanmoon.medium.com/leetcode-125-valid-palindrome-c-c-c-java-javascript-ruby-6912b2fb3ebb?source=rss-3bedef55bc1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6912b2fb3ebb</guid>
            <category><![CDATA[leetcode-125]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-easy]]></category>
            <category><![CDATA[leetcode-125-palindrome]]></category>
            <category><![CDATA[leetcode-valid-palindrome]]></category>
            <dc:creator><![CDATA[Chan Park]]></dc:creator>
            <pubDate>Fri, 02 Apr 2021 23:26:43 GMT</pubDate>
            <atom:updated>2021-04-03T00:29:22.018Z</atom:updated>
            <content:encoded><![CDATA[<p>Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> s = &quot;A man, a plan, a canal: Panama&quot;<br><strong>Output:</strong> true<br><strong>Explanation:</strong> &quot;amanaplanacanalpanama&quot; is a palindrome.</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> s = &quot;race a car&quot;<br><strong>Output:</strong> false<br><strong>Explanation:</strong> &quot;raceacar&quot; is not a palindrome.</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= s.length &lt;= 2 * 105</li><li>s consists only of printable ASCII characters.</li></ul><pre>//C<br>bool isPalindrome(char * s){<br>    int left=0, right=strlen(s)-1;<br>    while (left&lt;right) {<br>        while (left&lt;right &amp;&amp; !isalnum(s[left]))<br>            left++;<br>        while (left&lt;right &amp;&amp; !isalnum(s[right]))<br>            right--;<br>        if (tolower(s[left])!=tolower(s[right]))<br>            return false;<br>        left++;<br>        right--;<br>    }<br>    return true;<br>}</pre><pre>//C++<br>class Solution {<br>public:<br>    bool isPalindrome(string s) {<br>        int left=0, right=s.size()-1;<br>        while (left&lt;right) {<br>            while (left&lt;right &amp;&amp; !isalnum(s[left]))<br>                left++;<br>            while (left&lt;right &amp;&amp; !isalnum(s[right]))<br>                right--;<br>            if (tolower(s[left])!=tolower(s[right]))<br>                return false;<br>            left++;<br>            right--;<br>        }<br>        return true;<br>    }<br>};</pre><pre>//C#<br>// public class Solution {<br>//     public bool IsPalindrome(string s) {<br>//         StringBuilder sb = new StringBuilder();<br>        <br>//         foreach(Char c in s.ToCharArray())<br>//         {<br>//             if(char.IsLetterOrDigit(c) )<br>//             {<br>//                 sb.Append(c);<br>//             }<br>//         }<br>        <br>//         String str = sb.ToString().ToLower();<br>        <br>//         for(int i = 0; i &lt; str.Length/2; i++)<br>//         {<br>//             if(str[str.Length-1-i] !=  str[i])<br>//                 return false;<br>//         }<br>        <br>//         return true;<br>//     }<br>// }<br><br>using System.Text.RegularExpressions;<br><br>public class Solution {<br>    public bool IsPalindrome(string s) {<br><br>        s = Regex.Replace( s, &quot;[^a-zA-Z0-9]+&quot;, &quot;&quot;, RegexOptions.Compiled ).ToLower();<br><br>        for(int i = 0; i &lt; s.Length/2; i++)<br>        {<br>            if(s[s.Length-1-i] !=  s[i])<br>                return false;<br>        }<br>        <br>        return true;<br>    }<br>}</pre><pre>//Java<br>class Palindrome{<br>    public static void main(String[] args) {<br>        System.out.println(new Solution().isPalindrome(&quot;A man, a plan, a canal: Panama&quot;));<br>        System.out.println(new Solution1().isPalindrome(&quot;A man, a plan, a canal: Panama&quot;));<br>    }<br>}</pre><pre>class Solution {<br>    public boolean isPalindrome(String s) {<br>        StringBuilder sb = new StringBuilder();<br>        <br>        for(char c : s.toCharArray())<br>        {<br>            if(Character.isLetter(c) || Character.isDigit(c) )<br>            {<br>                sb.append(c);<br>            }<br>        }<br>        <br>        String str = sb.toString().toLowerCase();<br>        <br>        for(int i = 0; i &lt; str.length()/2; i++)<br>        {<br>            if(str.charAt(str.length()-1-i) !=  str.charAt(i))<br>                return false;<br>        }<br>        <br>        return true;<br>    }<br>}<br><br>class Solution1{<br>    public boolean isPalindrome(String s) {<br><br>                <br>        s = s.replaceAll(&quot;[^a-zA-Z0-9]&quot;,&quot;&quot;).toLowerCase();<br>        <br>        for(int i = 0; i &lt; s.length()/2; i++)<br>        {<br>            if(s.charAt(s.length()-1-i) !=  s.charAt(i))<br>                return false;<br>        }<br>        <br>        return true;<br>    }<br>}</pre><pre>//Javascript<br>var isPalindrome = function(s) {<br>    s = s.replace(/[^a-zA-Z0-9]/g,&#39;&#39;)<br>    s = s.toLowerCase()<br>    <br>    return s == s.split(&#39;&#39;).reverse().join(&#39;&#39;)<br>};<br><br>console.log(isPalindrome(&quot;A man, a plan, a canal: Panama&quot;));</pre><pre>//Ruby<br>def is_palindrome(s)<br>    s.downcase.gsub(/[^a-z\d]/,&quot;&quot;) == s.downcase.gsub(/[^a-z\d]/,&quot;&quot;).reverse <br>end<br><br>puts is_palindrome(&quot;A man, a plan, a canal: Panama&quot;);</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6912b2fb3ebb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 657 Robot Return to Origin (C, C++, C#, Java, Javascript, Ruby)]]></title>
            <link>https://thechanmoon.medium.com/leetcode-657-robot-return-to-origin-c-c-c-java-javascript-ruby-ad61e142aa0f?source=rss-3bedef55bc1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad61e142aa0f</guid>
            <category><![CDATA[leetcode-657]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[robot-return-to-origin]]></category>
            <category><![CDATA[leetcode-easy]]></category>
            <category><![CDATA[leetcode-657-robot]]></category>
            <dc:creator><![CDATA[Chan Park]]></dc:creator>
            <pubDate>Wed, 24 Mar 2021 19:27:13 GMT</pubDate>
            <atom:updated>2021-03-24T19:27:13.901Z</atom:updated>
            <content:encoded><![CDATA[<p>657. Robot Return to Origin</p><p>There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot <strong>ends up at (0, 0)</strong> after it completes its moves.</p><p>The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.</p><p><strong>Note</strong>: The way that the robot is “facing” is irrelevant. “R” will always make the robot move to the right once, “L” will always make it move left, etc. Also, assume that the magnitude of the robot’s movement is the same for each move.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> moves = &quot;UD&quot;<br><strong>Output:</strong> true<br><strong>Explanation</strong>: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> moves = &quot;LL&quot;<br><strong>Output:</strong> false<br><strong>Explanation</strong>: The robot moves left twice. It ends up two &quot;moves&quot; to the left of the origin. We return false because it is not at the origin at the end of its moves.</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> moves = &quot;RRDD&quot;<br><strong>Output:</strong> false</pre><p><strong>Example 4:</strong></p><pre><strong>Input:</strong> moves = &quot;LDRRLRUULR&quot;<br><strong>Output:</strong> false</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= moves.length &lt;= 2 * 104</li><li>moves only contains the characters &#39;U&#39;, &#39;D&#39;, &#39;L&#39; and &#39;R&#39;.</li></ul><p>//C</p><pre>bool judgeCircle(char * moves){<br>    <br>    int length = strlen(moves);<br>    <br>    int x = 0;<br>    int y = 0;<br>    int i = 0;<br>    <br>    for(i = 0; i &lt; length; i++)<br>    {<br>        if(moves[i] == &#39;R&#39;)<br>            x++;    <br>        else if(moves[i] == &#39;L&#39;)<br>            x--;<br>        else if(moves[i] == &#39;U&#39;)<br>            y++;    <br>        else if(moves[i] == &#39;D&#39;)<br>            y--;<br>    }<br>    return (x == 0 &amp;&amp; y == 0);<br>}</pre><pre>//C++<br>class Solution {<br>public:<br>    bool judgeCircle(string moves) {<br>        int x = 0;<br>        int y = 0;<br>        <br>        for(int i = 0; i &lt; moves.length(); i++)<br>        {<br>            if(moves[i] == &#39;R&#39;)<br>                x++;    <br>            else if(moves[i] == &#39;L&#39;)<br>                x--;<br>            else if(moves[i] == &#39;U&#39;)<br>                y++;    <br>            else if(moves[i] == &#39;D&#39;)<br>                y--;<br>        }<br>        return (x == 0 &amp;&amp; y == 0);<br>    }<br>};</pre><pre>//C#<br>public class Solution {<br>    public bool JudgeCircle(string moves) {<br>        int x = 0;<br>        int y = 0;<br>        <br>        char[] array = moves.ToCharArray();<br>        <br>        for(int i = 0; i &lt; array.Length; i++)<br>        {<br>            if(array[i] == &#39;R&#39;)<br>                x++;    <br>            else if(array[i] == &#39;L&#39;)<br>                x--;<br>            else if(array[i] == &#39;U&#39;)<br>                y++;    <br>            else if(array[i] == &#39;D&#39;)<br>                y--;<br>        }<br>        return (x == 0 &amp;&amp; y == 0);<br>    }<br>}</pre><pre>//Java<br>class Robot<br>{<br>    public static void main(String[] args) {<br>        Solution sl = new Solution();<br>        System.out.println(sl.judgeCircle(&quot;UD&quot;));<br>    }<br>}<br>class Solution {<br>    public boolean judgeCircle(String moves) {<br>        int x = 0;<br>        int y = 0;<br>        <br>        char[] array = moves.toCharArray();<br>        <br>        for(int i = 0; i &lt; array.length; i++)<br>        {<br>            if(array[i] == &#39;R&#39;)<br>                x++;    <br>            else if(array[i] == &#39;L&#39;)<br>                x--;<br>            else if(array[i] == &#39;U&#39;)<br>                y++;    <br>            else if(array[i] == &#39;D&#39;)<br>                y--;<br>        }<br>        return (x == 0 &amp;&amp; y == 0);<br>    }<br>}</pre><pre>//Javascript<br>var judgeCircle = function(moves) {<br>    let x = 0;<br>    let y = 0;<br>    <br>    let array = moves.split(&#39;&#39;);<br>    <br>    for(let i = 0; i &lt; array.length; i++)<br>    {<br>       if(array[i] == &#39;R&#39;)<br>           x++;    <br>       else if(array[i] == &#39;L&#39;)<br>           x--;<br>       else if(array[i] == &#39;U&#39;)<br>           y++;    <br>       else if(array[i] == &#39;D&#39;)<br>           y--;<br>    }<br>    return (x == 0 &amp;&amp; y == 0);<br>};<br><br>console.log(judgeCircle(&quot;UD&quot;));</pre><pre>//Ruby<br>def judge_circle(moves)<br>    (moves.count(&quot;R&quot;) == moves.count(&quot;L&quot;)) &amp;&amp; (moves.count(&quot;D&quot;) == moves.count(&quot;U&quot;))<br>end<br><br>puts judge_circle(&quot;UD&quot;);</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad61e142aa0f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 200. Number of Islands]]></title>
            <link>https://thechanmoon.medium.com/leetcode-200-number-of-islands-b13d365ae248?source=rss-3bedef55bc1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/b13d365ae248</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[number-of-islands]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[leetcode-200]]></category>
            <dc:creator><![CDATA[Chan Park]]></dc:creator>
            <pubDate>Fri, 19 Mar 2021 20:46:19 GMT</pubDate>
            <atom:updated>2021-03-19T20:46:19.870Z</atom:updated>
            <content:encoded><![CDATA[<p>Given an m x n 2D binary grid grid which represents a map of &#39;1&#39;s (land) and &#39;0&#39;s (water), return <em>the number of islands</em>.</p><p>An <strong>island</strong> is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> grid = [<br>  [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;],<br>  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;],<br>  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],<br>  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;]<br>]<br><strong>Output:</strong> 1</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> grid = [<br>  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],<br>  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],<br>  [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],<br>  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;]<br>]<br><strong>Output:</strong> 3</pre><p><strong>Constraints:</strong></p><ul><li>m == grid.length</li><li>n == grid[i].length</li><li>1 &lt;= m, n &lt;= 300</li><li>grid[i][j] is &#39;0&#39; or &#39;1&#39;.</li></ul><p>//C++</p><pre>class Solution {<br>public:<br>    int numIslands(vector&lt;vector&lt;char&gt;&gt;&amp; grid) {<br>        int numIsland = 0;<br><br>        for(int i = 0; i &lt; grid.size(); i++){<br>            for(int j = 0; j &lt; grid[0].size(); j++){<br>                if(grid[i][j] == &#39;1&#39;){<br>                    numIsland += dfs(grid, i, j);<br>                }<br>            }<br>        }<br>        return numIsland;<br>    }<br>    <br>    int dfs(vector&lt;vector&lt;char&gt;&gt;&amp; grid, int i, int j)<br>    {<br>        if(i &lt; 0 || i &gt;= grid.size() || j &lt; 0 || j &gt;= grid[i].size() || grid[i][j] == &#39;0&#39;){<br>            return 0;<br>        }<br>        grid[i][j] = &#39;0&#39;;<br>        dfs(grid, i + 1, j);<br>        dfs(grid, i - 1, j );<br>        dfs(grid, i, j + 1 );<br>        dfs(grid, i, j - 1 );<br><br>        return 1;<br>    }<br>};</pre><pre>//Java<br>public class NumIsland {<br><br>    public static void main(String[] args) {<br>        char[][] grid = { { &#39;1&#39;, &#39;1&#39;, &#39;1&#39;, &#39;1&#39;, &#39;0&#39; }, { &#39;1&#39;, &#39;1&#39;, &#39;0&#39;, &#39;1&#39;, &#39;0&#39; }, { &#39;1&#39;, &#39;1&#39;, &#39;0&#39;, &#39;0&#39;, &#39;0&#39; },<br>                { &#39;0&#39;, &#39;0&#39;, &#39;0&#39;, &#39;0&#39;, &#39;0&#39; } };<br>        System.out.println(new Solution().numIslands(grid));<br><br>    }<br>}<br><br>class Solution {<br>    public int numIslands(char[][] grid) {<br>        // if (grid == null || grid.length == 0) {<br>        // return 0;<br>        // }<br><br>        int numIsland = 0;<br><br>        for (int i = 0; i &lt; grid.length; i++) {<br>            for (int j = 0; j &lt; grid[0].length; j++) {<br>                if (grid[i][j] == &#39;1&#39;) {<br>                    dfs(grid, i, j);<br>                    numIsland++;<br>                }<br>            }<br>        }<br>        return numIsland;<br>    }<br><br>    public void dfs(char[][] grid, int i, int j) {<br>        if (i &lt; 0 || i &gt; grid.length - 1 || j &lt; 0 || j &gt; grid[i].length - 1 || grid[i][j] == &#39;0&#39;) {<br>            return;<br>        }<br>        grid[i][j] = &#39;0&#39;;<br>        dfs(grid, i + 1, j);<br>        dfs(grid, i - 1, j);<br>        dfs(grid, i, j + 1);<br>        dfs(grid, i, j - 1);<br><br>        // dfs(grid, i + 1, j + 1);<br>        // dfs(grid, i + 1, j - 1);<br>        // dfs(grid, i - 1, j + 1);<br>        // dfs(grid, i - 1, j - 1);<br>    }<br>}</pre><pre>//Javascript<br>var numIslands = function(grid) {<br>    // if (grid == null || grid.length == 0) {<br>    //     return 0;<br>    // }<br><br>    let numIsland = 0;<br><br>    for (let i = 0; i &lt; grid.length; i++) {<br>        for (let j = 0; j &lt; grid[0].length; j++) {<br>            if (grid[i][j] == &#39;1&#39;) {<br>                dfs(grid, i, j);<br>                numIsland++;<br>            }<br>        }<br>    }<br>    return numIsland;<br>};<br><br>var dfs = function(grid, i, j) {<br>    if (i &lt; 0 || i &gt;= grid.length || j &lt; 0 || j &gt;= grid[i].length || grid[i][j] == &#39;0&#39;) {<br>        return;<br>    }<br>    grid[i][j] = &#39;0&#39;;<br>    dfs(grid, i + 1, j);<br>    dfs(grid, i - 1, j);<br>    dfs(grid, i, j + 1);<br>    dfs(grid, i, j - 1);<br><br>    // dfs(grid, i + 1, j + 1);<br>    // dfs(grid, i + 1, j - 1);<br>    // dfs(grid, i - 1, j + 1);<br>    // dfs(grid, i - 1, j - 1);<br>}<br><br>let grid = [[ &#39;1&#39;, &#39;1&#39;, &#39;1&#39;, &#39;1&#39;, &#39;0&#39; ], [ &#39;1&#39;, &#39;1&#39;, &#39;0&#39;, &#39;1&#39;, &#39;0&#39; ], [ &#39;1&#39;, &#39;1&#39;, &#39;0&#39;, &#39;0&#39;, &#39;0&#39; ],<br>[ &#39;0&#39;, &#39;0&#39;, &#39;0&#39;, &#39;0&#39;, &#39;0&#39; ]];<br><br>console.log(numIslands(grid));</pre><pre>//C#<br>public class Solution {<br>    public int NumIslands(char[][] grid) {<br>        // if(grid == null || grid.Length == 0)<br>        // {<br>            // return 0;<br>        // }<br>        int numIsland = 0;<br><br>        for(int i = 0; i &lt; grid.Length; i++){<br>            for(int j = 0; j &lt; grid[0].Length; j++){<br>                if(grid[i][j] == &#39;1&#39;){<br>                    numIsland += dfs(grid, i, j);<br>                }<br>            }<br>        }<br>        return numIsland;<br>    }<br>    <br>    int dfs(char[][] grid, int i, int j)<br>    {<br>        if(i &lt; 0 || i &gt;= grid.Length || j &lt; 0 || j &gt;= grid[i].Length || grid[i][j] == &#39;0&#39;){<br>            return 0;<br>        }<br>        grid[i][j] = &#39;0&#39;;<br>        dfs(grid, i + 1, j);<br>        dfs(grid, i - 1, j );<br>        dfs(grid, i, j + 1 );<br>        dfs(grid, i, j - 1 );<br><br>        // dfs(grid, i + 1, j + 1);<br>        // dfs(grid, i + 1, j - 1);<br>        // dfs(grid, i - 1, j + 1);<br>        // dfs(grid, i - 1, j - 1);<br>        <br>        return 1;<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b13d365ae248" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leecode 217 Contains Duplicate ( C++,C#,Java,Javascript,Ruby)]]></title>
            <link>https://thechanmoon.medium.com/leecode-217-contains-duplicate-c-c-java-javascript-ruby-d949c90ae0a3?source=rss-3bedef55bc1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d949c90ae0a3</guid>
            <category><![CDATA[leetcode-217]]></category>
            <category><![CDATA[leetcode-containduplicate]]></category>
            <category><![CDATA[leetcode-easy]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Chan Park]]></dc:creator>
            <pubDate>Sat, 13 Mar 2021 02:34:44 GMT</pubDate>
            <atom:updated>2021-03-13T02:34:44.616Z</atom:updated>
            <content:encoded><![CDATA[<p>Contains Duplicate</p><p>Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.</p><p>Example 1:</p><p>Input: nums = [1,2,3,1] Output: true Example 2:</p><p>Input: nums = [1,2,3,4] Output: false Example 3:</p><p>Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true</p><p>Constraints:</p><p>1 &lt;= nums.length &lt;= 105 -109 &lt;= nums[i] &lt;= 109</p><pre>//C++<br>class Solution {<br>public:<br>    bool containsDuplicate(vector&lt;int&gt;&amp; nums) {<br>  <br>        unordered_set &lt;int&gt; hash ;<br>        <br>        for(int i = 0; i &lt; nums.size(); i++)<br>        {<br>            if(hash.find(nums[i]) != hash.end())<br>                return true;<br>            else<br>                hash.insert(nums[i]);<br>        }<br>        return false;<br>    }<br>};</pre><pre>//C#<br>public class Solution {<br>    public bool ContainsDuplicate(int[] nums) {<br>        HashSet&lt;int&gt; hash = new HashSet&lt;int&gt;();<br>        <br>        for(int i = 0; i &lt; nums.Length; i++)<br>        {<br>            if(hash.Contains(nums[i]))<br>                return true;<br>            else<br>                hash.Add(nums[i]);<br>        }<br>        return false;<br>    }<br>}</pre><pre>//Java<br>import java.util.*;<br>class ContainDuplicate{<br>    public static void main(String[] args) {<br>        Solution sl = new Solution();<br>        System.out.println(sl.containsDuplicate(new int[]{1,2,3,1}));<br>    }<br>}<br>class Solution {<br>    public boolean containsDuplicate(int[] nums) {<br>        HashSet&lt;Integer&gt; hash = new HashSet&lt;Integer&gt;();<br>        <br>        for(int i = 0; i &lt; nums.length; i++)<br>        {<br>            if(hash.contains(nums[i]))<br>                return true;<br>            else<br>                hash.add(nums[i]);<br>        }<br>        return false;<br>    }<br>}</pre><pre>//Javascript<br>var containsDuplicate = function(nums) {<br>        <br>    let set = new Set();<br><br>    for(let i = 0; i &lt; nums.length; i++)<br>    {<br>        if(set.has(nums[i]))<br>            return true;<br>        else<br>            set.add(nums[i]);<br>    }<br>    return false;<br>};<br><br>console.log(containsDuplicate([1,2,3,1]));</pre><pre>//Ruby<br>def contains_duplicate(nums)<br>    hash = {}<br>    <br>    nums.each do |x|<br>        if hash[x] == x<br>           return true <br>        end    <br>        hash[x] = x;<br>    end<br>    <br>    false<br>end</pre><pre>#def contains_duplicate(nums)<br>#    nums != nums.uniq<br>#end<br>puts contains_duplicate([1,2,3,1]);</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d949c90ae0a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 33. Search in Rotated Sorted Array]]></title>
            <link>https://thechanmoon.medium.com/leetcode-33-search-in-rotated-sorted-array-88c1aec3ea90?source=rss-3bedef55bc1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/88c1aec3ea90</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leecode-binary-search]]></category>
            <category><![CDATA[leetcode-33]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <dc:creator><![CDATA[Chan Park]]></dc:creator>
            <pubDate>Mon, 01 Mar 2021 03:59:29 GMT</pubDate>
            <atom:updated>2021-03-01T03:59:29.999Z</atom:updated>
            <content:encoded><![CDATA[<pre>There is an integer array nums sorted in ascending order (with distinct values).<br><br>Prior to being passed to your function, nums is rotated at an unknown pivot index k (0 &lt;= k &lt; nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].<br><br>Given the array nums after the rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.<br><br> <br><br>Example 1:<br><br>Input: nums = [4,5,6,7,0,1,2], target = 0<br>Output: 4<br>Example 2:<br><br>Input: nums = [4,5,6,7,0,1,2], target = 3<br>Output: -1<br>Example 3:<br><br>Input: nums = [1], target = 0<br>Output: -1<br> <br><br>Constraints:<br><br>1 &lt;= nums.length &lt;= 5000<br>-104 &lt;= nums[i] &lt;= 104<br>All values of nums are unique.<br>nums is guaranteed to be rotated at some pivot.<br>-104 &lt;= target &lt;= 104<br> <br><br>Follow up: Can you achieve this in O(log n) time complexity?</pre><pre>//Java<br>class BinarySearch{<br>    public static void main(String[] args) {<br>        System.out.println(new Solution().search(new int[]{4,5,6,7,0,1,2,3}, 0));<br>    }<br>}<br><br>class Solution {<br>    public int search(int[] nums, int target) {<br>        int left = 0, right = nums.length - 1;<br>        while (left &lt;= right) {<br>            int mid = (left + right) / 2;<br>            if (nums[mid] == target) <br>                return mid;<br><br>            if (nums[left] &lt;= nums[mid]) {// Left part<br>                if (target &gt;= nums[left] &amp;&amp; target &lt; nums[mid]) <br>                    right = mid - 1;<br>                else<br>                    left = mid + 1;<br>            }<br><br>            if (nums[mid] &lt;= nums[right]) {// Right part<br>                if (target &gt; nums[mid] &amp;&amp; target &lt;= nums[right])<br>                    left = mid + 1;<br>                else<br>                    right = mid - 1;<br>            }<br>        }		<br>        return -1;<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88c1aec3ea90" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leecode 704 Binary Search]]></title>
            <link>https://thechanmoon.medium.com/leecode-704-binary-search-3c89616ec9e4?source=rss-3bedef55bc1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c89616ec9e4</guid>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[leetcode-704]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-binary-search]]></category>
            <dc:creator><![CDATA[Chan Park]]></dc:creator>
            <pubDate>Fri, 26 Feb 2021 21:28:52 GMT</pubDate>
            <atom:updated>2021-02-27T14:22:23.645Z</atom:updated>
            <content:encoded><![CDATA[<pre>704. Binary Search<br><br>Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.<br><br><br>Example 1:<br><br>Input: nums = [-1,0,3,5,9,12], target = 9<br>Output: 4<br>Explanation: 9 exists in nums and its index is 4<br><br>Example 2:<br><br>Input: nums = [-1,0,3,5,9,12], target = 2<br>Output: -1<br>Explanation: 2 does not exist in nums so return -1<br> <br><br>Note:<br><br>You may assume that all elements in nums are unique.<br>n will be in the range [1, 10000].<br>The value of each element in nums will be in the range [-9999, 9999].</pre><pre>//Java<br>class BinarySearch {<br>    public static void main(String[] args) {<br>        System.out.println(new Solution().search(new int[] { -1, 0, 3, 5, 9, 12 }, 9));<br>    }<br>}</pre><pre>//C#<br>class Solution {<br>    public int search(int[] nums, int target) {<br><br>        if (nums.length == 0)<br>            return -1;<br><br>        int left = 0;<br>        int right = nums.length - 1;<br><br>        while (left &lt;= right) {<br>            int mid = left + (right - left) / 2;<br>            if (nums[mid] == target) {<br>                return mid;<br>            } else if (nums[mid] &gt; target) {<br>                right = mid - 1;<br>            } else {<br>                left = mid + 1;<br>            }<br>        }<br><br>        return -1;<br>    }<br>}</pre><pre>//Javascript<br>var search = function(nums, target) {<br>    if (nums.length == 0) <br>        return -1;<br><br>    let left = 0;<br>    let right = nums.length -1;<br><br>    while(left &lt;= right){<br>        let mid = left + Math.floor((right-left) /2);<br>        if(nums[mid] == target)<br>        {<br>            return mid;<br>        }else if (nums[mid] &gt; target)<br>        {<br>            right = mid -1;<br>        }else<br>        {<br>            left = mid +1;<br>        }<br>    }<br><br>    return -1;<br>};<br>console.log(search([-1,0,3,5,9,12],9));</pre><pre>public class Solution<br>{<br>    public int Search(int[] nums, int target)<br>    {<br>        if (nums.Length == 0)<br>            return -1;<br><br>        int left = 0;<br>        int right = nums.Length - 1;<br><br>        while (left &lt;= right)<br>        {<br>            int mid = left + (right - left) / 2;<br>            if (nums[mid] == target)<br>            {<br>                return mid;<br>            }<br>            else if (nums[mid] &gt; target)<br>            {<br>                right = mid - 1;<br>            }<br>            else<br>            {<br>                left = mid + 1;<br>            }<br>        }<br><br>        return -1;<br>    }<br>}</pre><pre>//C++<br>class Solution<br>{<br>public:<br>    int search(vector&lt;int&gt; &amp;nums, int target)<br>    {<br>        if (nums.size() == 0)<br>            return -1;<br><br>        int left = 0;<br>        int right = nums.size() - 1;<br><br>        while (left &lt;= right)<br>        {<br>            int mid = left + (right - left) / 2;<br>            if (nums[mid] == target)<br>            {<br>                return mid;<br>            }<br>            else if (nums[mid] &gt; target)<br>            {<br>                right = mid - 1;<br>            }<br>            else<br>            {<br>                left = mid + 1;<br>            }<br>        }<br><br>        return -1;<br>    }<br>};</pre><pre>//C<br>int search(int *nums, int numsSize, int target)<br>{    <br>    if (numsSize == 0) <br>        return -1;<br><br>    let left = 0;<br>    let right = numsSize -1;<br><br>    while(left &lt;= right){<br>        let mid = left + Math.floor((right-left) /2);<br>        if(nums[mid] == target)<br>        {<br>            return mid;<br>        }else if (nums[mid] &gt; target)<br>        {<br>            right = mid -1;<br>        }else<br>        {<br>            left = mid +1;<br>        }<br>    }<br><br>    return -1;<br>}</pre><pre>//Ruby<br>def search(nums, target)<br>    left = 0<br>    right = nums.size - 1<br>    while left &lt;= right<br>        mid = left + (right-left)/2<br>        <br>        if nums[mid] == target<br>        return mid<br>        elsif nums[mid] &gt; target<br>        right = mid - 1<br>        else<br>        left = mid + 1<br>        end<br>    end<br>return -1<br>end<br><br>def search1(nums, target)<br>    if nums.include?target<br>        return nums.index(target)<br>    end     <br>return -1<br>end    <br><br>puts(search([-1,0,3,5,9,12],9));<br>puts(search1([-1,0,3,5,9,12],9));</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c89616ec9e4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 34.Find First and Last Position of Element in Sorted Array]]></title>
            <link>https://thechanmoon.medium.com/leetcode-34-find-first-and-last-position-of-element-in-sorted-array-c5b68a60f4ca?source=rss-3bedef55bc1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c5b68a60f4ca</guid>
            <category><![CDATA[leetcode-34-javascript]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-34]]></category>
            <category><![CDATA[leetcode-34-java]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <dc:creator><![CDATA[Chan Park]]></dc:creator>
            <pubDate>Sat, 20 Feb 2021 02:58:28 GMT</pubDate>
            <atom:updated>2021-02-20T02:58:28.892Z</atom:updated>
            <content:encoded><![CDATA[<p>Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.</p><p>If target is not found in the array, return [-1, -1].</p><p><strong>Follow up:</strong> Could you write an algorithm with O(log n) runtime complexity?</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> nums = [5,7,7,8,8,10], target = 8<br><strong>Output:</strong> [3,4]</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> nums = [5,7,7,8,8,10], target = 6<br><strong>Output:</strong> [-1,-1]</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> nums = [], target = 0<br><strong>Output:</strong> [-1,-1]</pre><p><strong>Constraints:</strong></p><ul><li>0 &lt;= nums.length &lt;= 105</li><li>-109 &lt;= nums[i] &lt;= 109</li><li>nums is a non-decreasing array.</li><li>-109 &lt;= target &lt;= 109</li></ul><pre>//Java<br>import java.util.Arrays;<br><br>class SearchRange {<br>    public static void main(String[] args) {<br>        Solution sl = new Solution();<br>        System.out.println(Arrays.toString(sl.searchRange(new int[] { 1, 2, 3, 4, 5, 7, 7, 7, 7, 8 }, 7)));<br>    }<br>}<br>class Solution {<br>    public int[] searchRange(int[] nums, int target) {<br>        return new int[] { findFirst(nums, target), findLast(nums, target) };<br>    }<br><br>    public int findFirst(int[] nums, int target) {<br>        int index = -1;<br><br>        int start = 0;<br>        int end = nums.length - 1;<br><br>        while (start &lt;= end) {<br>            int mid = start + (end - start) / 2;<br>            if (nums[mid] &gt;= target) {<br>                end = mid - 1;<br>            } else {<br>                start = mid + 1;<br>            }<br>            if (nums[mid] == target)<br>                index = mid;<br>        }<br><br>        return index;<br>    }<br><br>    public int findLast(int[] nums, int target) {<br>        int index = -1;<br><br>        int start = 0;<br>        int end = nums.length - 1;<br><br>        while (start &lt;= end) {<br>            int mid = start + (end - start) / 2;<br>            if (nums[mid] &lt;= target) {<br>                start = mid + 1;<br>            } else {<br>                end = mid - 1;<br>            }<br>            if (nums[mid] == target)<br>                index = mid;<br>        }<br><br>        return index;<br>    }<br>}</pre><pre>//Javascript<br>var searchRange = function(nums, target) {<br>    return [findFirst(nums,target),findLast(nums,target)];<br>};<br><br>var findFirst = function(nums, target) {<br>    let index = -1;<br><br>    let start = 0;<br>    let end = nums.length - 1;<br>    <br>    while (start &lt;= end) {<br>        let mid = start + Math.floor((end - start) / 2);<br>        if (nums[mid] &gt;= target) {<br>            end = mid - 1;<br>        } else {<br>            start = mid + 1;<br>        }<br>        if (nums[mid] == target)<br>            index = mid;<br>    }<br>    <br>    return index;<br>};<br><br>var findLast = function(nums, target) {<br>    let index = -1;<br><br>    let start = 0;<br>    let end = nums.length - 1;<br><br>    while (start &lt;= end) {<br>        let mid = start + Math.floor((end - start) / 2);<br>        if (nums[mid] &lt;= target) {<br>            start = mid + 1;<br>        } else {<br>            end = mid - 1;<br>        }<br>        if (nums[mid] == target)<br>            index = mid;<br>    }<br><br>    return index;<br>};<br><br>console.log(searchRange([5,7,7,8,8,10],8))</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c5b68a60f4ca" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leecode 3 Longest Substring Without Repeating Characters]]></title>
            <link>https://thechanmoon.medium.com/leecode-3-longest-substring-without-repeating-characters-5b109d0676af?source=rss-3bedef55bc1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b109d0676af</guid>
            <category><![CDATA[leetcode-3]]></category>
            <category><![CDATA[leetcode-longestsubstring]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <dc:creator><![CDATA[Chan Park]]></dc:creator>
            <pubDate>Fri, 12 Feb 2021 18:59:48 GMT</pubDate>
            <atom:updated>2021-02-12T18:59:48.287Z</atom:updated>
            <content:encoded><![CDATA[<p>3. Longest Substring Without Repeating Characters</p><p>Given a string s, find the length of the <strong>longest substring</strong> without repeating characters.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> s = &quot;abcabcbb&quot;<br><strong>Output:</strong> 3<br><strong>Explanation:</strong> The answer is &quot;abc&quot;, with the length of 3.</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> s = &quot;bbbbb&quot;<br><strong>Output:</strong> 1<br><strong>Explanation:</strong> The answer is &quot;b&quot;, with the length of 1.</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> s = &quot;pwwkew&quot;<br><strong>Output:</strong> 3<br><strong>Explanation:</strong> The answer is &quot;wke&quot;, with the length of 3.<br>Notice that the answer must be a substring, &quot;pwke&quot; is a subsequence and not a substring.</pre><p><strong>Example 4:</strong></p><pre><strong>Input:</strong> s = &quot;&quot;<br><strong>Output:</strong> 0</pre><p><strong>Constraints:</strong></p><ul><li>0 &lt;= s.length &lt;= 5 * 104</li><li>s consists of English letters, digits, symbols and spaces.</li></ul><p>//Java</p><pre>import java.util.*;<br><br>class LongestSubstring {<br>    public static void main(String[] args) {<br>        Solution sl = new Solution();<br>        System.out.println(sl.lengthOfLongestSubstring(&quot;abcabcbb&quot;));<br>    }<br>}<br>class Solution {<br>    public int lengthOfLongestSubstring(String s) {<br>        int i = 0;<br>        int j = 0;<br>        int max = 0;<br><br>        HashSet&lt;Character&gt; hashset = new HashSet&lt;Character&gt;();<br><br>        for (j = 0; j &lt; s.length();) {<br>            if (!hashset.contains(s.charAt(j))) {<br>                hashset.add(s.charAt(j++));<br>                max = Math.max(hashset.size(), max);<br>            } else {<br>                hashset.remove(s.charAt(i++));<br>            }<br>        }<br>        return max;<br>    }<br>}</pre><pre>//Javascript<br>var lengthOfLongestSubstring = function(s) {<br>    let i = 0;<br>    let j = 0;<br>    let max = 0;<br><br>    let hashset = new Set();<br><br>    for (j = 0; j &lt; s.length;) {<br>        if (!hashset.has(s[j]))<br>        {<br>            hashset.add(s[j++]);<br>            max = Math.max(hashset.size, max);<br>        }<br>        else<br>        {<br>            hashset.delete(s[i++]);<br>        }<br>    }<br>    return max;<br>};<br><br>console.log(lengthOfLongestSubstring(&quot;abcabcbb&quot;));</pre><pre>//CSharp<br>public class Solution<br>{<br>    public int LengthOfLongestSubstring(string s)<br>    {<br>        int i = 0;<br>        int j = 0;<br>        int max = 0;<br><br>        HashSet&lt;Char&gt; hashset = new HashSet&lt;Char&gt;();<br><br>        for (j = 0; j &lt; s.Length;)<br>        {<br>            if (!hashset.Contains(s[j]))<br>            {<br>                hashset.Add(s[j++]);<br>                max = Math.Max(hashset.Count, max);<br>            }<br>            else<br>            {<br>                hashset.Remove(s[i++]);<br>            }<br>        }<br>        return max;<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b109d0676af" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leecode 949. Largest Time for Given Digits]]></title>
            <link>https://thechanmoon.medium.com/leecode-949-largest-time-for-given-digits-d0fe65bc179b?source=rss-3bedef55bc1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0fe65bc179b</guid>
            <category><![CDATA[leetcode-949]]></category>
            <category><![CDATA[leetcode-largest-time]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Chan Park]]></dc:creator>
            <pubDate>Fri, 05 Feb 2021 18:24:26 GMT</pubDate>
            <atom:updated>2021-02-05T18:24:26.808Z</atom:updated>
            <content:encoded><![CDATA[<p>Given an array arr of 4 digits, find the latest 24-hour time that can be made using each digit <strong>exactly once</strong>.</p><p>24-hour times are formatted as &quot;HH:MM&quot;, where HH is between 00 and 23, and MM is between 00 and 59. The earliest 24-hour time is 00:00, and the latest is 23:59.</p><p>Return <em>the latest 24-hour time in </em><em>&quot;HH:MM&quot; format</em>. If no valid time can be made, return an empty string.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> A = [1,2,3,4]<br><strong>Output:</strong> &quot;23:41&quot;<br><strong>Explanation:</strong> The valid 24-hour times are &quot;12:34&quot;, &quot;12:43&quot;, &quot;13:24&quot;, &quot;13:42&quot;, &quot;14:23&quot;, &quot;14:32&quot;, &quot;21:34&quot;, &quot;21:43&quot;, &quot;23:14&quot;, and &quot;23:41&quot;. Of these times, &quot;23:41&quot; is the latest.</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> A = [5,5,5,5]<br><strong>Output:</strong> &quot;&quot;<br><strong>Explanation:</strong> There are no valid 24-hour times as &quot;55:55&quot; is not valid.</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> A = [0,0,0,0]<br><strong>Output:</strong> &quot;00:00&quot;</pre><p><strong>Example 4:</strong></p><pre><strong>Input:</strong> A = [0,0,1,0]<br><strong>Output:</strong> &quot;10:00&quot;</pre><p><strong>Constraints:</strong></p><ul><li>arr.length == 4</li><li>0 &lt;= arr[i] &lt;= 9</li></ul><pre>//Java<br>class LargestTime{<br>    public static void main(String[] args) {<br>        Solution sl = new Solution();<br>        System.out.println(sl.largestTimeFromDigits(new int[]{1,2,3,4}));<br>        System.out.println(sl.largestTimeFromDigits(new int[]{5,5,5,5}));<br>        System.out.println(sl.largestTimeFromDigits(new int[]{0,0,0,0}));<br>        System.out.println(sl.largestTimeFromDigits(new int[]{0,0,1,0}));<br>    }<br>}<br><br>class Solution {<br>    private int max_time = -1;<br><br>    public String largestTimeFromDigits(int[] A) {<br>        this.max_time = -1;<br>        permutate(A, 0);<br>        if (this.max_time == -1)<br>            return &quot;&quot;;<br>        else<br>            return String.format(&quot;%02d:%02d&quot;, max_time / 60, max_time % 60);<br>    }<br><br>    protected void permutate(int[] array, int start) {<br>        if (start == array.length) {<br>            this.build_time(array);<br>            return;<br>        }<br>        for (int i = start; i &lt; array.length; ++i) {<br>            this.swap(array, i, start);<br>            this.permutate(array, start + 1);<br>            this.swap(array, i, start);<br>        }<br>    }<br><br>    protected void build_time(int[] perm) {<br>        int hour = perm[0] * 10 + perm[1];<br>        int minute = perm[2] * 10 + perm[3];<br>        if (hour &lt; 24 &amp;&amp; minute &lt; 60)<br>            this.max_time = Math.max(this.max_time, hour * 60 + minute);<br>    }<br><br>    protected void swap(int[] array, int i, int j) {<br>        if (i != j) {<br>            int temp = array[i];<br>            array[i] = array[j];<br>            array[j] = temp;<br>        }<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0fe65bc179b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode 1647 Minimum Deletions to Make Character Frequencies Unique]]></title>
            <link>https://thechanmoon.medium.com/leetcode-1647-minimum-deletions-to-make-character-frequencies-unique-2af8a156f120?source=rss-3bedef55bc1d------2</link>
            <guid isPermaLink="false">https://medium.com/p/2af8a156f120</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[leetcode-medium]]></category>
            <category><![CDATA[leetcode-1647]]></category>
            <dc:creator><![CDATA[Chan Park]]></dc:creator>
            <pubDate>Fri, 29 Jan 2021 17:05:35 GMT</pubDate>
            <atom:updated>2021-01-29T17:05:35.366Z</atom:updated>
            <content:encoded><![CDATA[<p>A string s is called <strong>good</strong> if there are no two different characters in s that have the same <strong>frequency</strong>.</p><p>Given a string s, return<em> the </em><strong><em>minimum</em></strong><em> number of characters you need to delete to make </em>s<em> </em><strong><em>good</em></strong><em>.</em></p><p>The <strong>frequency</strong> of a character in a string is the number of times it appears in the string. For example, in the string &quot;aab&quot;, the <strong>frequency</strong> of &#39;a&#39; is 2, while the <strong>frequency</strong> of &#39;b&#39; is 1.</p><p><strong>Example 1:</strong></p><pre><strong>Input:</strong> s = &quot;aab&quot;<br><strong>Output:</strong> 0<br><strong>Explanation:</strong> s is already good.</pre><p><strong>Example 2:</strong></p><pre><strong>Input:</strong> s = &quot;aaabbbcc&quot;<br><strong>Output:</strong> 2<br><strong>Explanation:</strong> You can delete two &#39;b&#39;s resulting in the good string &quot;aaabcc&quot;.<br>Another way it to delete one &#39;b&#39; and one &#39;c&#39; resulting in the good string &quot;aaabbc&quot;.</pre><p><strong>Example 3:</strong></p><pre><strong>Input:</strong> s = &quot;ceabaacb&quot;<br><strong>Output:</strong> 2<br><strong>Explanation:</strong> You can delete both &#39;c&#39;s resulting in the good string &quot;eabaab&quot;.<br>Note that we only care about characters that are still in the string at the end (i.e. frequency of 0 is ignored).</pre><p><strong>Constraints:</strong></p><ul><li>1 &lt;= s.length &lt;= 105</li><li>s contains only lowercase English letters.</li></ul><pre>//Java<br>import java.util.*;<br><br>class MinDeletions {<br><br>    public static void main(String[] args) {<br>        Solution sl = new Solution();<br>    }<br>}<br><br>class Solution {<br>    public int minDeletions(String s) {<br>        int[] freq = new int[26];<br>        for (char c : s.toCharArray()) {<br>            freq[c - &#39;a&#39;]++;<br>        }<br>        Arrays.sort(freq);<br>        int exp = freq[25];<br>        int res = 0;<br><br>        for (int i = 25; i &gt;= 0; i--) {<br>            if (freq[i] == 0)<br>                break;<br>            if (freq[i] &gt; exp) {<br>                res += freq[i] - exp;<br>            } else {<br>                exp = freq[i];<br>            }<br>            if (exp &gt; 0) {<br>                exp--; // Lowest exp is zero, cannot be negative<br>            }<br>        }<br>        return res;<br>    }<br>}</pre><pre>//Javascript<br>var minDeletions = function(s) {<br>    <br>    let freq = [...new Array(26)].map(()=&gt;0);<br>    <br>    let arr = s.split(&#39;&#39;);<br>    for(let i = 0; i &lt; arr.length; i++)<br>    {<br>        freq[arr[i].charCodeAt() - &#39;a&#39;.charCodeAt()]++;<br>    }<br>    <br>    // console.log(freq);<br>    freq.sort((a,b)=&gt;a-b);<br>    // freq.sort();<br>    let exp = freq[25];<br>    let res = 0;<br>    <br>    for(let i = 25; i &gt;-1; i--)<br>    {<br>        if( freq[i] == 0 )<br>            break;<br>        <br>        if(freq[i] &gt; exp)<br>        {<br>            res += freq[i] - exp;<br>        }else<br>        {<br>            exp = freq[i];        <br>        }<br>        <br>        if(exp &gt; 0)<br>            exp--;<br>    }<br>    return res;<br>};<br><br>console.log(minDeletions(&quot;aaabbbcc&quot;));</pre><pre>//C#<br>public class Solution {<br>    public int MinDeletions(string s) {<br>        int[] freq = new int[26];<br>        <br>        // char[] arr;<br>        // arr = s.ToCharArray(0, s.Length);<br>        <br>        foreach (char c in s.ToCharArray(0, s.Length)) {<br>            freq[c - &#39;a&#39;]++;<br>        }<br>		Array.Sort(freq);<br>        int exp = freq[25];<br>        int res = 0;<br>        for (int i = 25; i &gt;= 0; i--) {<br>            if (freq[i] == 0) break;<br>            if (freq[i] &gt; exp) {<br>                res += freq[i] - exp;<br>            } else {<br>                exp = freq[i];<br>            }<br>            if (exp &gt; 0) exp--; // Lowest exp is zero, cannot be negative<br>        }<br>        return res;<br>    }<br>}</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2af8a156f120" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>