<?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"><channel><title><![CDATA[Shohei Yoshida's Blog]]></title><description><![CDATA[Shohei Yoshida's Blog]]></description><link>https://syohex.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 11:13:00 GMT</lastBuildDate><atom:link href="https://syohex.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[LeetCode 2840. Check if Strings Can be Made Equal With Operations II in F#]]></title><description><![CDATA[Check if Strings Can be Made Equal With Operations II - LeetCode]]></description><link>https://syohex.hashnode.dev/leetcode-2840-check-if-strings-can-be-made-equal-with-operations-ii-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-2840-check-if-strings-can-be-made-equal-with-operations-ii-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Mon, 30 Mar 2026 01:22:41 GMT</pubDate><content:encoded><![CDATA[<h2>URL</h2>
<p><a href="https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-ii/description/?envType=daily-question&amp;envId=2026-03-30">Check if Strings Can be Made Equal With Operations II - LeetCode</a></p>
<h2>Code</h2>
<p><a href="https://github.com/syohex/dotnet-study/tree/main/fsharp/leetcode/challenge/202603/check_if_strings_can_be_made_equal_with_operations2/main.fsx">https://github.com/syohex/dotnet-study/tree/main/fsharp/leetcode/challenge/202603/check_if_strings_can_be_made_equal_with_operations2/main.fsx</a></p>
<pre><code class="language-plaintext">let checkStrings (s1: string) (s2: string) : bool =
    let updateMap key1 key2 m =
        let v1 = Map.tryFind key1 m |&gt; Option.defaultValue 0
        let m = Map.add key1 (v1 + 1) m
        let v2 = Map.tryFind key2 m |&gt; Option.defaultValue 0
        Map.add key2 (v2 - 1) m

    let rec checkStrings' cs1 cs2 evens odds =
        match cs1, cs2 with
        | [], [] -&gt;
            let allZero = Map.values &gt;&gt; Seq.forall ((=) 0)
            allZero evens &amp;&amp; allZero odds
        | [], _
        | _, [] -&gt; failwith "never reach here"
        | (i, c1) :: t1, c2 :: t2 -&gt;
            if i % 2 = 0 then
                checkStrings' t1 t2 (updateMap c1 c2 evens) odds
            else
                checkStrings' t1 t2 evens (updateMap c1 c2 odds)

    let cs1 = s1 |&gt; Seq.toList |&gt; List.indexed
    let cs2 = s2 |&gt; Seq.toList
    checkStrings' cs1 cs2 Map.empty Map.empty
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 3713. Longest Balanced Substring I in F#]]></title><description><![CDATA[URL Code let longes...]]></description><link>https://syohex.hashnode.dev/leetcode-3713-longest-balanced-substring-i-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-3713-longest-balanced-substring-i-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Thu, 12 Feb 2026 05:18:27 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/longest-balanced-substring-i/description/?envType=daily-question&amp;envId=2026-02-12">https://leetcode.com/problems/longest-balanced-substring-i/description/?envType=daily-question&amp;envId=2026-02-12</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/main/fsharp/leetcode/challenge/202602/longest_balanced_substring1/main.fsx">https://github.com/syohex/dotnet-study/blob/main/fsharp/leetcode/challenge/202602/longest_balanced_substring1/main.fsx</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> longestBalanced (s: string) : int =
    <span class="hljs-keyword">let</span> isBalanced cs =
        cs
        |&gt; Seq.fold
            (<span class="hljs-keyword">fun</span> (acc, count) c -&gt;
                <span class="hljs-keyword">let</span> v = Map.tryFind c acc |&gt; Option.defaultValue <span class="hljs-number">0</span> |&gt; ((+) <span class="hljs-number">1</span>)
                <span class="hljs-keyword">let</span> acc = Map.add c v acc
                acc, max v count)
            (Map.empty, <span class="hljs-number">0</span>)
        |&gt; <span class="hljs-keyword">fun</span> (freq, count) -&gt; freq |&gt; Map.forall (<span class="hljs-keyword">fun</span> _ v -&gt; v = count)

    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> f i acc =
        <span class="hljs-keyword">if</span> i &gt; s.Length <span class="hljs-keyword">then</span>
            acc
        <span class="hljs-keyword">else</span>
            <span class="hljs-keyword">let</span> ok = s |&gt; Seq.windowed i |&gt; Seq.exists isBalanced
            <span class="hljs-keyword">if</span> ok <span class="hljs-keyword">then</span> f (i + <span class="hljs-number">1</span>) i <span class="hljs-keyword">else</span> f (i + <span class="hljs-number">1</span>) acc

    f <span class="hljs-number">1</span> <span class="hljs-number">0</span>
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 3634. Minimum Removals to Balance Array in F#]]></title><description><![CDATA[Minimum Removals to Balance Array - LeetCode dotnet-study/fsharp/leetcode/challenge/202602/minimum_removals_to_balance_array/main.fsx at main ·…]]></description><link>https://syohex.hashnode.dev/leetcode-3634-minimum-removals-to-balance-array-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-3634-minimum-removals-to-balance-array-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Fri, 06 Feb 2026 02:50:01 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/minimum-removals-to-balance-array/description/?envType=daily-question&amp;envId=2026-02-06">Minimum Removals to Balance Array - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/main/fsharp/leetcode/challenge/202602/minimum_removals_to_balance_array/main.fsx">dotnet-study/fsharp/leetcode/challenge/202602/minimum_removals_to_balance_array/main.fsx at main · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> minRemoval (nums: int list) (k: int) : int =
    <span class="hljs-keyword">let</span> k = int64 k

    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> adjustWindow (nums: int64[]) left right : int =
        <span class="hljs-keyword">if</span> left &gt;= right <span class="hljs-keyword">then</span>
            printfn <span class="hljs-string">"hige"</span>
            left
        <span class="hljs-keyword">elif</span> nums.[left] * k &lt; nums.[right] <span class="hljs-keyword">then</span>
            adjustWindow nums (left + <span class="hljs-number">1</span>) right
        <span class="hljs-keyword">else</span>
            left

    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> minRemoval' (nums: int64[]) left right acc =
        <span class="hljs-keyword">if</span> right &gt;= nums.Length <span class="hljs-keyword">then</span>
            acc
        <span class="hljs-keyword">else</span>
            <span class="hljs-keyword">let</span> left = adjustWindow nums left right
            <span class="hljs-keyword">let</span> acc = min acc (nums.Length - (right - left + <span class="hljs-number">1</span>))
            minRemoval' nums left (right + <span class="hljs-number">1</span>) acc

    <span class="hljs-keyword">let</span> nums = nums |&gt; List.map int64 |&gt; List.sort |&gt; List.toArray
    minRemoval' nums <span class="hljs-number">0</span> <span class="hljs-number">0</span> System.Int32.MaxValue
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 3650. Minimum Cost Path with Edge Reversals in F#]]></title><description><![CDATA[Minimum Cost Path with Edge Reversals - LeetCode dotnet-study/fsharp/leetcode/challenge/202601/minimum_cost_path_with_edge_reversals/main.fsx at main ·…]]></description><link>https://syohex.hashnode.dev/leetcode-3650-minimum-cost-path-with-edge-reversals-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-3650-minimum-cost-path-with-edge-reversals-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Tue, 27 Jan 2026 02:40:01 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/minimum-cost-path-with-edge-reversals/description/?envType=daily-question&amp;envId=2026-01-27">Minimum Cost Path with Edge Reversals - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/main/fsharp/leetcode/challenge/202601/minimum_cost_path_with_edge_reversals/main.fsx">dotnet-study/fsharp/leetcode/challenge/202601/minimum_cost_path_with_edge_reversals/main.fsx at main · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-class"><span class="hljs-keyword">type</span> <span class="hljs-title">Edge</span> </span>= { node: int; cost: int }

<span class="hljs-keyword">let</span> toGraph (edges: (int * int * int) list) : Map&lt;int, Edge list&gt; =
    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> f edges acc =
        <span class="hljs-keyword">match</span> edges <span class="hljs-keyword">with</span>
        | [] -&gt; acc
        | (from, to_, cost) :: t -&gt;
            <span class="hljs-keyword">let</span> edge = { node = to_; cost = cost }

            <span class="hljs-keyword">let</span> acc =
                <span class="hljs-keyword">match</span> Map.tryFind from acc <span class="hljs-keyword">with</span>
                | Some v -&gt; Map.add from (edge :: v) acc
                | None -&gt; Map.add from [ edge ] acc

            <span class="hljs-keyword">let</span> reverseEdge = { node = from; cost = cost * <span class="hljs-number">2</span> }

            <span class="hljs-keyword">let</span> acc =
                <span class="hljs-keyword">match</span> Map.tryFind to_ acc <span class="hljs-keyword">with</span>
                | Some v -&gt; Map.add to_ (reverseEdge :: v) acc
                | None -&gt; Map.add to_ [ reverseEdge ] acc

            f t acc

    f edges Map.empty

<span class="hljs-keyword">let</span> minCost (n: int) (edges: (int * int * int) list) : int =
    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> f q (graph: Map&lt;int, Edge list&gt;) (costs: Map&lt;int, int&gt;) =
        <span class="hljs-keyword">match</span> q <span class="hljs-keyword">with</span>
        | [] -&gt; Map.tryFind (n - <span class="hljs-number">1</span>) costs |&gt; Option.defaultValue <span class="hljs-number">-1</span>
        | (node, cost) :: t -&gt;
            <span class="hljs-keyword">let</span> nexts = Map.tryFind node graph |&gt; Option.defaultValue []

            <span class="hljs-keyword">let</span> q, costs =
                nexts
                |&gt; List.fold
                    (<span class="hljs-keyword">fun</span> (q, costs) { node = nextNode; cost = nextCost } -&gt;
                        <span class="hljs-keyword">let</span> newCost = cost + nextCost

                        <span class="hljs-keyword">match</span> Map.tryFind nextNode costs <span class="hljs-keyword">with</span>
                        | None -&gt; (nextNode, newCost) :: q, Map.add nextNode newCost costs
                        | Some v -&gt;
                            <span class="hljs-keyword">if</span> newCost &lt; v <span class="hljs-keyword">then</span>
                                (nextNode, newCost) :: q, Map.add nextNode newCost costs
                            <span class="hljs-keyword">else</span>
                                q, costs)
                    (t, costs)

            f q graph costs


    <span class="hljs-keyword">let</span> graph = toGraph edges
    f [ (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>) ] graph Map.empty
</code></pre>
]]></content:encoded></item><item><title><![CDATA[3315. Construct the Minimum Bitwise Array II in F#]]></title><description><![CDATA[Construct the Minimum Bitwise Array II - LeetCode]]></description><link>https://syohex.hashnode.dev/3315-construct-the-minimum-bitwise-array-ii-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/3315-construct-the-minimum-bitwise-array-ii-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Wed, 21 Jan 2026 02:04:26 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/construct-the-minimum-bitwise-array-ii/description/?envType=daily-question&amp;envId=2026-01-21">Construct the Minimum Bitwise Array II - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/tree/main/fsharp/leetcode/challenge/202601/construct_the_minimum_bitwise_array2/main.fsx">https://github.com/syohex/dotnet-study/tree/main/fsharp/leetcode/challenge/202601/construct_the_minimum_bitwise_array2/main.fsx</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> minBitwiseArray (nums: int list) : int list =
    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> f n mask =
        <span class="hljs-keyword">if</span> n &amp;&amp;&amp; mask = <span class="hljs-number">0</span> <span class="hljs-keyword">then</span>
            n &amp;&amp;&amp; ~~~(mask &gt;&gt;&gt; <span class="hljs-number">1</span>)
        <span class="hljs-keyword">else</span>
            f n (mask &lt;&lt;&lt; <span class="hljs-number">1</span>)

    nums |&gt; List.map (<span class="hljs-keyword">fun</span> n -&gt; <span class="hljs-keyword">if</span> n % <span class="hljs-number">2</span> = <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> <span class="hljs-number">-1</span> <span class="hljs-keyword">else</span> f n <span class="hljs-number">1</span>)
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 1161. Maximum Level Sum of a Binary Tree in F#]]></title><description><![CDATA[Maximum Level Sum of a Binary Tree - LeetCode dotnet-study/fsharp/leetcode/challenge/202601/maximum_level_sum_of_a_binary_tree/main.fsx at main ·…]]></description><link>https://syohex.hashnode.dev/leetcode-1161-maximum-level-sum-of-a-binary-tree-in-f-1</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-1161-maximum-level-sum-of-a-binary-tree-in-f-1</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Tue, 06 Jan 2026 02:38:00 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/description/?envType=daily-question&amp;envId=2026-01-06">Maximum Level Sum of a Binary Tree - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/main/fsharp/leetcode/challenge/202601/maximum_level_sum_of_a_binary_tree/main.fsx">dotnet-study/fsharp/leetcode/challenge/202601/maximum_level_sum_of_a_binary_tree/main.fsx at main · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-class"><span class="hljs-keyword">type</span> <span class="hljs-title">TreeNode</span> </span>=
    | Leaf
    | Node <span class="hljs-keyword">of</span> int * TreeNode * TreeNode

<span class="hljs-keyword">let</span> maxLevelSum (root: TreeNode) : int =
    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> maxLevelSum' (nodes: TreeNode list) level maxSum maxLevel =
        <span class="hljs-keyword">match</span> nodes <span class="hljs-keyword">with</span>
        | [] -&gt; maxLevel
        | _ -&gt;
            <span class="hljs-keyword">let</span> sum =
                nodes
                |&gt; List.map (<span class="hljs-keyword">fun</span> n -&gt;
                    <span class="hljs-keyword">match</span> n <span class="hljs-keyword">with</span>
                    | Leaf -&gt; <span class="hljs-number">0</span>
                    | Node(v, _, _) -&gt; v)
                |&gt; List.sum

            <span class="hljs-keyword">let</span> nodes =
                nodes
                |&gt; List.fold
                    (<span class="hljs-keyword">fun</span> acc n -&gt;
                        <span class="hljs-keyword">match</span> n <span class="hljs-keyword">with</span>
                        | Leaf -&gt; acc
                        | Node(_, left, right) -&gt; right :: left :: acc)
                    []
                |&gt; List.filter ((&lt;&gt;) Leaf)

            <span class="hljs-keyword">let</span> maxSum, maxLevel = <span class="hljs-keyword">if</span> sum &gt; maxSum <span class="hljs-keyword">then</span> sum, level <span class="hljs-keyword">else</span> maxSum, maxLevel
            maxLevelSum' nodes (level + <span class="hljs-number">1</span>) maxSum maxLevel

    maxLevelSum' [ root ] <span class="hljs-number">1</span> System.Int32.MinValue <span class="hljs-number">1</span>
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 3075. Maximize Happiness of Selected Children in F#]]></title><description><![CDATA[Maximize Happiness of Selected Children - LeetCode dotnet-study/fsharp/leetcode/challenge/202512/maxmize_happiness_of_selected_children/main.fsx at main ·…]]></description><link>https://syohex.hashnode.dev/leetcode-3075-maximize-happiness-of-selected-children-in-f-1</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-3075-maximize-happiness-of-selected-children-in-f-1</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Thu, 25 Dec 2025 03:36:36 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/maximize-happiness-of-selected-children/description/?envType=daily-question&amp;envId=2025-12-25">Maximize Happiness of Selected Children - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/main/fsharp/leetcode/challenge/202512/maxmize_happiness_of_selected_children/main.fsx">dotnet-study/fsharp/leetcode/challenge/202512/maxmize_happiness_of_selected_children/main.fsx at main · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> maximumHappinessSum (happiness: int list) (k: int) : int64 =
    happiness
    |&gt; List.sort
    |&gt; List.rev
    |&gt; List.indexed
    |&gt; List.take k
    |&gt; List.fold (<span class="hljs-keyword">fun</span> acc (i, v) -&gt; acc + int64 (max (v - i) <span class="hljs-number">0</span>)) <span class="hljs-number">0</span>L
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 2110. Number of Smooth Descent Periods of a Stock in F#]]></title><description><![CDATA[Number of Smooth Descent Periods of a Stock - LeetCode dotnet-study/fsharp/leetcode/challenge/202512/number_of_smooth_descent_periods_of_a_stock/main.fsx…]]></description><link>https://syohex.hashnode.dev/leetcode-2110-number-of-smooth-descent-periods-of-a-stock-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-2110-number-of-smooth-descent-periods-of-a-stock-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Mon, 15 Dec 2025 03:51:19 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/description/?envType=daily-question&amp;envId=2025-12-15">Number of Smooth Descent Periods of a Stock - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202512/number_of_smooth_descent_periods_of_a_stock/main.fsx">dotnet-study/fsharp/leetcode/challenge/202512/number_of_smooth_descent_periods_of_a_stock/main.fsx at master · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> getDescentPeriods (prices: int list) : int64 =
    <span class="hljs-keyword">let</span> len = List.length prices

    <span class="hljs-keyword">let</span> countCombinations left right =
        <span class="hljs-keyword">let</span> count = right - left
        (<span class="hljs-number">1</span> + count) * count / <span class="hljs-number">2</span>

    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> getDescentPeriods' prices prev left acc =
        <span class="hljs-keyword">match</span> prices <span class="hljs-keyword">with</span>
        | [] -&gt; acc + (int64 &lt;| countCombinations left len)
        | (i, h) :: t -&gt;
            <span class="hljs-keyword">if</span> h = prev - <span class="hljs-number">1</span> <span class="hljs-keyword">then</span>
                getDescentPeriods' t h left acc
            <span class="hljs-keyword">else</span>
                <span class="hljs-keyword">let</span> acc = acc + (int64 &lt;| countCombinations left i)
                getDescentPeriods' t h i acc

    <span class="hljs-keyword">let</span> prev = List.head prices
    <span class="hljs-keyword">let</span> prices = prices |&gt; List.indexed |&gt; List.skip <span class="hljs-number">1</span>
    getDescentPeriods' prices prev <span class="hljs-number">0</span> <span class="hljs-number">0</span>L
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 3623. Count Number of Trapezoids in F#]]></title><description><![CDATA[Count Number of Trapezoids I - LeetCode dotnet-study/fsharp/leetcode/challenge/202512/count_number_of_trapezoids1/main.fsx at master · syohex/dotnet-study]]></description><link>https://syohex.hashnode.dev/leetcode-3623-count-number-of-trapezoids-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-3623-count-number-of-trapezoids-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Tue, 02 Dec 2025 03:42:36 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/count-number-of-trapezoids-i/description/?envType=daily-question&amp;envId=2025-12-02">Count Number of Trapezoids I - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202512/count_number_of_trapezoids1/main.fsx">dotnet-study/fsharp/leetcode/challenge/202512/count_number_of_trapezoids1/main.fsx at master · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> countTrapezoids (points: (int * int) list) : int =
    <span class="hljs-keyword">let</span> modulo = <span class="hljs-number">1</span>_000_000_007L

    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> countTrapezoids' ys totalEdges acc =
        <span class="hljs-keyword">match</span> ys <span class="hljs-keyword">with</span>
        | [] -&gt; acc % modulo |&gt; int
        | h :: t -&gt;
            <span class="hljs-keyword">let</span> edges = (h * (h - <span class="hljs-number">1</span>)) / <span class="hljs-number">2</span>
            <span class="hljs-keyword">let</span> acc = acc + int64 edges * totalEdges
            <span class="hljs-keyword">let</span> totalEdges = totalEdges + int64 edges
            countTrapezoids' t totalEdges acc

    <span class="hljs-keyword">let</span> ys =
        points
        |&gt; List.fold
            (<span class="hljs-keyword">fun</span> acc (_, y) -&gt;
                <span class="hljs-keyword">match</span> Map.tryFind y acc <span class="hljs-keyword">with</span>
                | Some v -&gt; Map.add y (v + <span class="hljs-number">1</span>) acc
                | None -&gt; Map.add y <span class="hljs-number">1</span> acc)
            Map.empty
        |&gt; Map.values
        |&gt; Seq.toList

    countTrapezoids' ys <span class="hljs-number">0</span>L <span class="hljs-number">0</span>L
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 2435. Paths in Matrix Whose Sum Is Divisible by K in F#]]></title><description><![CDATA[Paths in Matrix Whose Sum Is Divisible by K - LeetCode dotnet-study/fsharp/leetcode/challenge/202511/paths_in_matrix_whose_sum_is_divisible_by_key/main.fsx…]]></description><link>https://syohex.hashnode.dev/leetcode-2435-paths-in-matrix-whose-sum-is-divisible-by-k-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-2435-paths-in-matrix-whose-sum-is-divisible-by-k-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Wed, 26 Nov 2025 06:45:45 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k/description/?envType=daily-question&amp;envId=2025-11-26">Paths in Matrix Whose Sum Is Divisible by K - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202511/paths_in_matrix_whose_sum_is_divisible_by_key/main.fsx">dotnet-study/fsharp/leetcode/challenge/202511/paths_in_matrix_whose_sum_is_divisible_by_key/main.fsx at master · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> numberOfPaths (grid: int[,]) (k: int) : int =
    <span class="hljs-keyword">let</span> rows, cols = Array2D.length1 grid, Array2D.length2 grid
    <span class="hljs-keyword">let</span> modulo = <span class="hljs-number">1</span>_000_000_007

    <span class="hljs-keyword">let</span> getValue row col m dp =
        dp |&gt; Map.tryFind (row, col, m) |&gt; Option.defaultValue <span class="hljs-number">0</span>

    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> numberOfPaths' row col dp =
        <span class="hljs-keyword">if</span> row &gt;= rows <span class="hljs-keyword">then</span>
            Map.find (rows - <span class="hljs-number">1</span>, cols - <span class="hljs-number">1</span>, <span class="hljs-number">0</span>) dp
        <span class="hljs-keyword">elif</span> col &gt;= cols <span class="hljs-keyword">then</span>
            numberOfPaths' (row + <span class="hljs-number">1</span>) <span class="hljs-number">0</span> dp
        <span class="hljs-keyword">else</span>
            <span class="hljs-keyword">let</span> m = grid.[row, col] % k

            <span class="hljs-keyword">let</span> dp =
                seq { <span class="hljs-number">0</span> .. (k - <span class="hljs-number">1</span>) }
                |&gt; Seq.fold
                    (<span class="hljs-keyword">fun</span> acc i -&gt;
                        <span class="hljs-keyword">let</span> prev = (i + k - m) % k
                        <span class="hljs-keyword">let</span> v1 = getValue (row - <span class="hljs-number">1</span>) col prev acc
                        <span class="hljs-keyword">let</span> v2 = getValue row (col - <span class="hljs-number">1</span>) prev acc
                        <span class="hljs-keyword">let</span> v = (v1 + v2) % modulo
                        Map.add (row, col, i) v acc)
                    dp

            numberOfPaths' row (col + <span class="hljs-number">1</span>) dp

    <span class="hljs-keyword">let</span> dp = Map.empty |&gt; Map.add (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, grid.[<span class="hljs-number">0</span>, <span class="hljs-number">0</span>] % k) <span class="hljs-number">1</span>
    numberOfPaths' <span class="hljs-number">0</span> <span class="hljs-number">1</span> dp
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 1930. Unique Length-3 Palindromic Subsequences in F#]]></title><description><![CDATA[Unique Length-3 Palindromic Subsequences - LeetCode]]></description><link>https://syohex.hashnode.dev/leetcode-1930-unique-length-3-palindromic-subsequences-in-f-1-1</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-1930-unique-length-3-palindromic-subsequences-in-f-1-1</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Fri, 21 Nov 2025 05:52:51 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/unique-length-3-palindromic-subsequences/description/?envType=daily-question&amp;envId=2025-11-21">Unique Length-3 Palindromic Subsequences - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202511/unique_length3_palindromic_subsequences/main.fsx">https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202511/unique_length3_palindromic_subsequences/main.fsx</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> findStartAndLastPosition (c: char) (s: string) : int * int =
    s
    |&gt; Seq.indexed
    |&gt; Seq.fold
        (<span class="hljs-keyword">fun</span> (first, last) (i, d) -&gt;
            <span class="hljs-keyword">if</span> c = d <span class="hljs-keyword">then</span>
                <span class="hljs-keyword">if</span> first = <span class="hljs-number">-1</span> <span class="hljs-keyword">then</span> i, last <span class="hljs-keyword">else</span> first, i
            <span class="hljs-keyword">else</span>
                first, last)
        (<span class="hljs-number">-1</span>, <span class="hljs-number">-1</span>)

<span class="hljs-keyword">let</span> countPalindromicSubsequence (s: string) : int =
    s
    |&gt; Set.ofSeq
    |&gt; Set.map (<span class="hljs-keyword">fun</span> c -&gt; findStartAndLastPosition c s)
    |&gt; Set.filter (<span class="hljs-keyword">fun</span> (_, last) -&gt; last &lt;&gt; <span class="hljs-number">-1</span>)
    |&gt; Set.fold
        (<span class="hljs-keyword">fun</span> acc (first, last) -&gt;
            <span class="hljs-keyword">let</span> uniqueChars = s.Substring(first + <span class="hljs-number">1</span>, last - first - <span class="hljs-number">1</span>) |&gt; Set.ofSeq |&gt; Set.count
            acc + uniqueChars)
        <span class="hljs-number">0</span>
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 1513. Number of Substrings With Only 1s in F#]]></title><description><![CDATA[Number of Substrings With Only 1s - LeetCode]]></description><link>https://syohex.hashnode.dev/leetcode-1513-number-of-substrings-with-only-1s-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-1513-number-of-substrings-with-only-1s-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Sun, 16 Nov 2025 05:35:56 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/number-of-substrings-with-only-1s/description/?envType=daily-question&amp;envId=2025-11-16">Number of Substrings With Only 1s - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202511/number_of_substrings_with_only_1s/main.fsx">https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202511/number_of_substrings_with_only_1s/main.fsx</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> numSub (s: string) : int =
    <span class="hljs-keyword">let</span> modulo = <span class="hljs-number">1</span>_000_000_007L

    <span class="hljs-keyword">let</span> sum (n: int64) : int64 = ((n * (<span class="hljs-number">1</span>L + n)) / <span class="hljs-number">2</span>L) % modulo

    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> numSub' cs ones acc =
        <span class="hljs-keyword">match</span> cs <span class="hljs-keyword">with</span>
        | [] -&gt; acc + sum ones |&gt; int
        | h :: t -&gt;
            <span class="hljs-keyword">if</span> h = '<span class="hljs-number">1</span>' <span class="hljs-keyword">then</span>
                numSub' t (ones + <span class="hljs-number">1</span>L) acc
            <span class="hljs-keyword">else</span>
                numSub' t <span class="hljs-number">0</span> (acc + sum ones)

    numSub' (Seq.toList s) <span class="hljs-number">0</span> <span class="hljs-number">0</span>
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 1578. Minimum Time to Make Rope Colorful in F#]]></title><description><![CDATA[Minimum Time to Make Rope Colorful - LeetCode dotnet-study/fsharp/leetcode/challenge/202511/minimum_time_to_make_rope_colorful/main.fsx at master ·…]]></description><link>https://syohex.hashnode.dev/leetcode-1578-minimum-time-to-make-rope-colorful-in-f-1</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-1578-minimum-time-to-make-rope-colorful-in-f-1</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Mon, 03 Nov 2025 04:50:21 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/minimum-time-to-make-rope-colorful/description/?envType=daily-question&amp;envId=2025-11-03">Minimum Time to Make Rope Colorful - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/6e9fb9d95d12502ac230eb45e300a03553802a99/fsharp/leetcode/challenge/202511/minimum_time_to_make_rope_colorful/main.fsx">dotnet-study/fsharp/leetcode/challenge/202511/minimum_time_to_make_rope_colorful/main.fsx at master · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> minCost (colors: string) (neededTime: int list) : int =
    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> minCost' ct prev maxVal sum acc =
        <span class="hljs-keyword">match</span> ct <span class="hljs-keyword">with</span>
        | [] -&gt; acc + (sum - maxVal)
        | (time, color) :: t -&gt;
            <span class="hljs-keyword">if</span> color = prev <span class="hljs-keyword">then</span>
                minCost' t prev (max time maxVal) (sum + time) acc
            <span class="hljs-keyword">else</span>
                minCost' t color time time (acc + sum - maxVal)


    <span class="hljs-keyword">let</span> ct = colors |&gt; Seq.toList |&gt; List.zip neededTime
    minCost' ct '?' <span class="hljs-number">0</span> <span class="hljs-number">0</span> <span class="hljs-number">0</span>
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 2125. Number of Laser Beams in a Bank in F#]]></title><description><![CDATA[Number of Laser Beams in a Bank - LeetCode dotnet-study/fsharp/leetcode/challenge/202510/number_of_laster_beams_in_a_bank/main.fsx at master ·…]]></description><link>https://syohex.hashnode.dev/leetcode-2125-number-of-laser-beams-in-a-bank-in-f-1</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-2125-number-of-laser-beams-in-a-bank-in-f-1</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Mon, 27 Oct 2025 02:38:22 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/number-of-laser-beams-in-a-bank/description/?envType=daily-question&amp;envId=2025-10-27">Number of Laser Beams in a Bank - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202510/number_of_laster_beams_in_a_bank/main.fsx">dotnet-study/fsharp/leetcode/challenge/202510/number_of_laster_beams_in_a_bank/main.fsx at master · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> numberOfBeams (bank: string list) : int =
    bank
    |&gt; List.map (<span class="hljs-keyword">fun</span> s -&gt; s |&gt; Seq.filter ((=) '<span class="hljs-number">1</span>') |&gt; Seq.length)
    |&gt; List.filter ((&lt;&gt;) <span class="hljs-number">0</span>)
    |&gt; List.fold (<span class="hljs-keyword">fun</span> (acc, prev) n -&gt; acc + prev * n, n) (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
    |&gt; fst
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 3147. Taking Maximum Energy From the Mystic Dungeon in F#]]></title><description><![CDATA[Taking Maximum Energy From the Mystic Dungeon - LeetCode dotnet-study/fsharp/leetcode/challenge/202510/taking_maximum_energy_from_the_mystic_dungeon/main.f…]]></description><link>https://syohex.hashnode.dev/leetcode-3147-taking-maximum-energy-from-the-mystic-dungeon-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-3147-taking-maximum-energy-from-the-mystic-dungeon-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Fri, 10 Oct 2025 00:44:48 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/taking-maximum-energy-from-the-mystic-dungeon/description/?envType=daily-question&amp;envId=2025-10-10">Taking Maximum Energy From the Mystic Dungeon - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202510/taking_maximum_energy_from_the_mystic_dungeon/main.fsx">dotnet-study/fsharp/leetcode/challenge/202510/taking_maximum_energy_from_the_mystic_dungeon/main.fsx at master · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> maximumEnergy (energy: int list) (k: int) : int =
    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> maximumEnergy' i (dp: int[]) (energy: int[]) acc =
        <span class="hljs-keyword">if</span> i &lt; <span class="hljs-number">0</span> <span class="hljs-keyword">then</span>
            acc
        <span class="hljs-keyword">else</span>
            <span class="hljs-keyword">if</span> i + k &gt;= energy.Length <span class="hljs-keyword">then</span>
                dp.[i] &lt;- energy.[i]
            <span class="hljs-keyword">else</span>
                dp.[i] &lt;- dp.[i + k] + energy.[i]

            maximumEnergy' (i - <span class="hljs-number">1</span>) dp energy (max acc dp.[i])

    <span class="hljs-keyword">let</span> energy = Array.ofList energy
    <span class="hljs-keyword">let</span> len = energy.Length
    <span class="hljs-keyword">let</span> dp = Array.zeroCreate len
    maximumEnergy' (len - <span class="hljs-number">1</span>) dp energy System.Int32.MinValue
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 2300. Successful Pairs of Spells and Potions in F#]]></title><description><![CDATA[Successful Pairs of Spells and Potions - LeetCode dotnet-study/fsharp/leetcode/challenge/202510/successful_pairs_of_spells_and_potions/main.fsx at master ·…]]></description><link>https://syohex.hashnode.dev/leetcode-2300-successful-pairs-of-spells-and-potions-in-f-1</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-2300-successful-pairs-of-spells-and-potions-in-f-1</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Wed, 08 Oct 2025 02:40:29 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/successful-pairs-of-spells-and-potions/description/?envType=daily-question&amp;envId=2025-10-08">Successful Pairs of Spells and Potions - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202510/successful_pairs_of_spells_and_potions/main.fsx">dotnet-study/fsharp/leetcode/challenge/202510/successful_pairs_of_spells_and_potions/main.fsx at master · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> successfulPairs (spells: int list) (potions: int list) (success: int64) : int list =
    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> lowerBound left right spell (vals: int[]) =
        <span class="hljs-keyword">if</span> left &gt;= right <span class="hljs-keyword">then</span>
            left
        <span class="hljs-keyword">else</span>
            <span class="hljs-keyword">let</span> mid = left + (right - left) / <span class="hljs-number">2</span>

            <span class="hljs-keyword">if</span> int64 (spell * vals.[mid]) &lt; success <span class="hljs-keyword">then</span>
                lowerBound (mid + <span class="hljs-number">1</span>) right spell vals
            <span class="hljs-keyword">else</span>
                lowerBound left mid spell vals

    <span class="hljs-keyword">let</span> potions = potions |&gt; List.sort |&gt; List.toArray
    <span class="hljs-keyword">let</span> len = potions.Length

    spells
    |&gt; List.map (<span class="hljs-keyword">fun</span> spell -&gt;
        <span class="hljs-keyword">let</span> pos = lowerBound <span class="hljs-number">0</span> len spell potions
        len - pos)
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 2221. Find Triangular Sum of an Array in F#]]></title><description><![CDATA[Find Triangular Sum of an Array - LeetCode dotnet-study/fsharp/leetcode/challenge/202509/find_triangular_sum_of_an_array/main.fsx at master ·…]]></description><link>https://syohex.hashnode.dev/leetcode-2221-find-triangular-sum-of-an-array-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-2221-find-triangular-sum-of-an-array-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Tue, 30 Sep 2025 04:34:31 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/find-triangular-sum-of-an-array/description/?envType=daily-question&amp;envId=2025-09-30">Find Triangular Sum of an Array - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202509/find_triangular_sum_of_an_array/main.fsx">dotnet-study/fsharp/leetcode/challenge/202509/find_triangular_sum_of_an_array/main.fsx at master · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> triangleSum (nums: int list) : int =
    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> triangleSum' nums =
        <span class="hljs-keyword">match</span> nums <span class="hljs-keyword">with</span>
        | [] -&gt; failwith <span class="hljs-string">"never reach here"</span>
        | h :: [] -&gt; h
        | _ -&gt;
            <span class="hljs-keyword">let</span> nums = nums |&gt; List.windowed <span class="hljs-number">2</span> |&gt; List.map (<span class="hljs-keyword">fun</span> v -&gt; (List.sum v) % <span class="hljs-number">10</span>)
            triangleSum' nums

    triangleSum' nums
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 165. Compare Version Numbers in F#]]></title><description><![CDATA[Compare Version Numbers - LeetCode dotnet-study/fsharp/leetcode/challenge/202509/compare_version_numbers/main.fsx at master · syohex/dotnet-study]]></description><link>https://syohex.hashnode.dev/leetcode-165-compare-version-numbers-in-f-1</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-165-compare-version-numbers-in-f-1</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Tue, 23 Sep 2025 02:03:42 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/compare-version-numbers/description/?envType=daily-question&amp;envId=2025-09-23">Compare Version Numbers - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202509/compare_version_numbers/main.fsx">dotnet-study/fsharp/leetcode/challenge/202509/compare_version_numbers/main.fsx at master · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> compareVersion (version1: string) (version2: string) : int =
    <span class="hljs-keyword">let</span> toNumber s =
        <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> toNumber' cs acc =
            <span class="hljs-keyword">match</span> cs <span class="hljs-keyword">with</span>
            | [] -&gt; acc
            | h :: t -&gt; toNumber' t (acc * <span class="hljs-number">10</span> + int h - int '<span class="hljs-number">0</span>')

        toNumber' (Seq.toList s) <span class="hljs-number">0</span>

    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> compareVersion' v1 v2 =
        <span class="hljs-keyword">match</span> v1, v2 <span class="hljs-keyword">with</span>
        | [], [] -&gt; <span class="hljs-number">0</span>
        | h1 :: t1, [] -&gt;
            <span class="hljs-keyword">let</span> n1 = toNumber h1
            <span class="hljs-keyword">if</span> n1 = <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> compareVersion' t1 [] <span class="hljs-keyword">else</span> compare n1 <span class="hljs-number">0</span>
        | [], h2 :: t2 -&gt;
            <span class="hljs-keyword">let</span> n2 = toNumber h2
            <span class="hljs-keyword">if</span> n2 = <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> compareVersion' t2 [] <span class="hljs-keyword">else</span> compare <span class="hljs-number">0</span> n2
        | h1 :: t1, h2 :: t2 -&gt;
            <span class="hljs-keyword">let</span> n1, n2 = toNumber h1, toNumber h2
            <span class="hljs-keyword">if</span> n1 = n2 <span class="hljs-keyword">then</span> compareVersion' t1 t2 <span class="hljs-keyword">else</span> compare n1 n2

    <span class="hljs-keyword">let</span> v1 = version1.Split('.') |&gt; Array.toList
    <span class="hljs-keyword">let</span> v2 = version2.Split('.') |&gt; Array.toList
    compareVersion' v1 v2
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 2785. Sort Vowels in a String in F#]]></title><description><![CDATA[Sort Vowels in a String - LeetCode dotnet-study/fsharp/leetcode/challenge/202509/sort_vowels_in_a_string/main.fsx at master · syohex/dotnet-study]]></description><link>https://syohex.hashnode.dev/leetcode-2785-sort-vowels-in-a-string-in-f-1</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-2785-sort-vowels-in-a-string-in-f-1</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Thu, 11 Sep 2025 04:27:00 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/sort-vowels-in-a-string/description/?envType=daily-question&amp;envId=2025-09-11">Sort Vowels in a String - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202509/sort_vowels_in_a_string/main.fsx">dotnet-study/fsharp/leetcode/challenge/202509/sort_vowels_in_a_string/main.fsx at master · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> sortVowels (s: string) : string =
    <span class="hljs-keyword">let</span> isVowel c =
        <span class="hljs-keyword">match</span> c <span class="hljs-keyword">with</span>
        | <span class="hljs-symbol">'a</span>'
        | <span class="hljs-symbol">'e</span>'
        | <span class="hljs-symbol">'i</span>'
        | <span class="hljs-symbol">'o</span>'
        | <span class="hljs-symbol">'u</span>'
        | <span class="hljs-symbol">'A</span>'
        | <span class="hljs-symbol">'E</span>'
        | <span class="hljs-symbol">'I</span>'
        | <span class="hljs-symbol">'O</span>'
        | <span class="hljs-symbol">'U</span>' -&gt; <span class="hljs-keyword">true</span>
        | _ -&gt; <span class="hljs-keyword">false</span>

    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> sortVowels' cs vowels (acc: char list) =
        <span class="hljs-keyword">match</span> cs <span class="hljs-keyword">with</span>
        | [] -&gt; acc |&gt; List.rev |&gt; System.String.Concat
        | h :: t -&gt;
            <span class="hljs-keyword">if</span> isVowel h <span class="hljs-keyword">then</span>
                sortVowels' t (List.tail vowels) (List.head vowels :: acc)
            <span class="hljs-keyword">else</span>
                sortVowels' t vowels (h :: acc)

    <span class="hljs-keyword">let</span> vowels = s |&gt; Seq.filter isVowel |&gt; Seq.sort |&gt; Seq.toList
    sortVowels' (Seq.toList s) vowels []
</code></pre>
]]></content:encoded></item><item><title><![CDATA[LeetCode 1317. Convert Integer to the Sum of Two No-Zero Integers in F#]]></title><description><![CDATA[Convert Integer to the Sum of Two No-Zero Integers - LeetCode dotnet-study/fsharp/leetcode/challenge/202509/convert_integer_to_the_sum_of_two_no_zero_integ…]]></description><link>https://syohex.hashnode.dev/leetcode-1317-convert-integer-to-the-sum-of-two-no-zero-integers-in-f</link><guid isPermaLink="true">https://syohex.hashnode.dev/leetcode-1317-convert-integer-to-the-sum-of-two-no-zero-integers-in-f</guid><dc:creator><![CDATA[Shohei Yoshida]]></dc:creator><pubDate>Mon, 08 Sep 2025 03:20:18 GMT</pubDate><content:encoded><![CDATA[<h2 id="heading-url">URL</h2>
<p><a target="_blank" href="https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/description/?envType=daily-question&amp;envId=2025-09-08">Convert Integer to the Sum of Two No-Zero Integers - LeetCode</a></p>
<h2 id="heading-code">Code</h2>
<p><a target="_blank" href="https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/challenge/202509/convert_integer_to_the_sum_of_two_no_zero_integers/main.fsx">dotnet-study/fsharp/leetcode/challenge/202509/convert_integer_to_the_sum_of_two_no_zero_integers/main.fsx at master · syohex/dotnet-study</a></p>
<pre><code class="lang-fsharp"><span class="hljs-keyword">let</span> getNoZeroIntegers (n: int) : int * int =
    <span class="hljs-keyword">let</span> isNoZeroInteger n = string n |&gt; Seq.forall ((&lt;&gt;) '<span class="hljs-number">0</span>')

    <span class="hljs-keyword">let</span> <span class="hljs-keyword">rec</span> getNoZeroIntegers' a =
        <span class="hljs-keyword">if</span> a = n <span class="hljs-keyword">then</span>
            failwith <span class="hljs-string">"never reach here"</span>
        <span class="hljs-keyword">else</span>
            <span class="hljs-keyword">let</span> b = n - a

            <span class="hljs-keyword">if</span> isNoZeroInteger a &amp;&amp; isNoZeroInteger b <span class="hljs-keyword">then</span>
                a, b
            <span class="hljs-keyword">else</span>
                getNoZeroIntegers' (a + <span class="hljs-number">1</span>)

    getNoZeroIntegers' <span class="hljs-number">1</span>
</code></pre>
]]></content:encoded></item></channel></rss>