mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
update
This commit is contained in:
parent
9930064684
commit
55953eb844
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2022.03.30**
|
||||
> 最后更新日期: **2022.04.03**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
171
leetcode-cn/originData/encrypt-and-decrypt-strings.json
Normal file
171
leetcode-cn/originData/encrypt-and-decrypt-strings.json
Normal file
File diff suppressed because one or more lines are too long
164
leetcode-cn/originData/find-players-with-zero-or-one-losses.json
Normal file
164
leetcode-cn/originData/find-players-with-zero-or-one-losses.json
Normal file
File diff suppressed because one or more lines are too long
158
leetcode-cn/originData/find-triangular-sum-of-an-array.json
Normal file
158
leetcode-cn/originData/find-triangular-sum-of-an-array.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
158
leetcode-cn/originData/minimum-bit-flips-to-convert-number.json
Normal file
158
leetcode-cn/originData/minimum-bit-flips-to-convert-number.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
160
leetcode-cn/originData/number-of-ways-to-select-buildings.json
Normal file
160
leetcode-cn/originData/number-of-ways-to-select-buildings.json
Normal file
File diff suppressed because one or more lines are too long
158
leetcode-cn/originData/sum-of-scores-of-built-strings.json
Normal file
158
leetcode-cn/originData/sum-of-scores-of-built-strings.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,62 @@
|
||||
<p>给你一个字符数组 <code>keys</code> ,由若干 <strong>互不相同</strong> 的字符组成。还有一个字符串数组 <code>values</code> ,内含若干长度为 2 的字符串。另给你一个字符串数组 <code>dictionary</code> ,包含解密后所有允许的原字符串。请你设计并实现一个支持加密及解密下标从 <strong>0</strong> 开始字符串的数据结构。</p>
|
||||
|
||||
<p>字符串 <strong>加密</strong> 按下述步骤进行:</p>
|
||||
|
||||
<ol>
|
||||
<li>对字符串中的每个字符 <code>c</code> ,先从 <code>keys</code> 中找出满足 <code>keys[i] == c</code> 的下标 <code>i</code> 。</li>
|
||||
<li>在字符串中,用 <code>values[i]</code> 替换字符 <code>c</code> 。</li>
|
||||
</ol>
|
||||
|
||||
<p>字符串 <strong>解密</strong> 按下述步骤进行:</p>
|
||||
|
||||
<ol>
|
||||
<li>将字符串每相邻 2 个字符划分为一个子字符串,对于每个子字符串 <code>s</code> ,找出满足 <code>values[i] == s</code> 的一个下标 <code>i</code> 。如果存在多个有效的 <code>i</code> ,从中选择 <strong>任意</strong> 一个。这意味着一个字符串解密可能得到多个解密字符串。</li>
|
||||
<li>在字符串中,用 <code>keys[i]</code> 替换 <code>s</code> 。</li>
|
||||
</ol>
|
||||
|
||||
<p>实现 <code>Encrypter</code> 类:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>Encrypter(char[] keys, String[] values, String[] dictionary)</code> 用 <code>keys</code>、<code>values</code> 和 <code>dictionary</code> 初始化 <code>Encrypter</code> 类。</li>
|
||||
<li><code>String encrypt(String word1)</code> 按上述加密过程完成对 <code>word1</code> 的加密,并返回加密后的字符串。</li>
|
||||
<li><code>int decrypt(String word2)</code> 统计并返回可以由 <code>word2</code> 解密得到且出现在 <code>dictionary</code> 中的字符串数目。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>
|
||||
["Encrypter", "encrypt", "decrypt"]
|
||||
[[['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]], ["abcd"], ["eizfeiam"]]
|
||||
<strong>输出:</strong>
|
||||
[null, "eizfeiam", 2]
|
||||
|
||||
<strong>解释:</strong>
|
||||
Encrypter encrypter = new Encrypter([['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]);
|
||||
encrypter.encrypt("abcd"); // 返回 "eizfeiam"。
|
||||
// 'a' 映射为 "ei",'b' 映射为 "zf",'c' 映射为 "ei",'d' 映射为 "am"。
|
||||
encrypter.decrypt("eizfeiam"); // return 2.
|
||||
// "ei" 可以映射为 'a' 或 'c',"zf" 映射为 'b',"am" 映射为 'd'。
|
||||
// 因此,解密后可以得到的字符串是 "abad","cbad","abcd" 和 "cbcd"。
|
||||
// 其中 2 个字符串,"abad" 和 "abcd",在 dictionary 中出现,所以答案是 2 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= keys.length == values.length <= 26</code></li>
|
||||
<li><code>values[i].length == 2</code></li>
|
||||
<li><code>1 <= dictionary.length <= 100</code></li>
|
||||
<li><code>1 <= dictionary[i].length <= 100</code></li>
|
||||
<li>所有 <code>keys[i]</code> 和 <code>dictionary[i]</code> <strong>互不相同</strong></li>
|
||||
<li><code>1 <= word1.length <= 2000</code></li>
|
||||
<li><code>1 <= word2.length <= 200</code></li>
|
||||
<li>所有 <code>word1[i]</code> 都出现在 <code>keys</code> 中</li>
|
||||
<li><code>word2.length</code> 是偶数</li>
|
||||
<li><code>keys</code>、<code>values[i]</code>、<code>dictionary[i]</code>、<code>word1</code> 和 <code>word2</code> 只含小写英文字母</li>
|
||||
<li>至多调用 <code>encrypt</code> 和 <code>decrypt</code> <strong>总计</strong> <code>200</code> 次</li>
|
||||
</ul>
|
@ -0,0 +1,54 @@
|
||||
<p>给你一个整数数组 <code>matches</code> 其中 <code>matches[i] = [winner<sub>i</sub>, loser<sub>i</sub>]</code> 表示在一场比赛中 <code>winner<sub>i</sub></code> 击败了 <code>loser<sub>i</sub></code> 。</p>
|
||||
|
||||
<p>返回一个长度为 2 的列表<em> </em><code>answer</code> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>answer[0]</code> 是所有 <strong>没有</strong> 输掉任何比赛的玩家列表。</li>
|
||||
<li><code>answer[1]</code> 是所有恰好输掉 <strong>一场</strong> 比赛的玩家列表。</li>
|
||||
</ul>
|
||||
|
||||
<p>两个列表中的值都应该按 <strong>递增</strong> 顺序返回。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>只考虑那些参与 <strong>至少一场</strong> 比赛的玩家。</li>
|
||||
<li>生成的测试用例保证 <strong>不存在</strong> 两场比赛结果 <strong>相同</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
|
||||
<strong>输出:</strong>[[1,2,10],[4,5,7,8]]
|
||||
<strong>解释:</strong>
|
||||
玩家 1、2 和 10 都没有输掉任何比赛。
|
||||
玩家 4、5、7 和 8 每个都输掉一场比赛。
|
||||
玩家 3、6 和 9 每个都输掉两场比赛。
|
||||
因此,answer[0] = [1,2,10] 和 answer[1] = [4,5,7,8] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>matches = [[2,3],[1,3],[5,4],[6,4]]
|
||||
<strong>输出:</strong>[[1,2,5,6],[]]
|
||||
<strong>解释:</strong>
|
||||
玩家 1、2、5 和 6 都没有输掉任何比赛。
|
||||
玩家 3 和 4 每个都输掉两场比赛。
|
||||
因此,answer[0] = [1,2,5,6] 和 answer[1] = [] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= matches.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>matches[i].length == 2</code></li>
|
||||
<li><code>1 <= winner<sub>i</sub>, loser<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
<li><code>winner<sub>i</sub> != loser<sub>i</sub></code></li>
|
||||
<li>所有 <code>matches[i]</code> <strong>互不相同</strong></li>
|
||||
</ul>
|
@ -0,0 +1,41 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ,其中 <code>nums[i]</code> 是 <code>0</code> 到 <code>9</code> 之间(两者都包含)的一个数字。</p>
|
||||
|
||||
<p><code>nums</code> 的 <strong>三角和</strong> 是执行以下操作以后最后剩下元素的值:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>nums</code> 初始包含 <code>n</code> 个元素。如果 <code>n == 1</code> ,<strong>终止</strong> 操作。否则,<strong>创建</strong> 一个新的下标从 <strong>0</strong> 开始的长度为 <code>n - 1</code> 的整数数组 <code>newNums</code> 。</li>
|
||||
<li>对于满足 <code>0 <= i < n - 1</code> 的下标 <code>i</code> ,<code>newNums[i]</code> <strong>赋值</strong> 为 <code>(nums[i] + nums[i+1]) % 10</code> ,<code>%</code> 表示取余运算。</li>
|
||||
<li>将 <code>newNums</code> <strong>替换</strong> 数组 <code>nums</code> 。</li>
|
||||
<li>从步骤 1 开始 <strong>重复</strong> 整个过程。</li>
|
||||
</ol>
|
||||
|
||||
<p>请你返回 <code>nums</code> 的三角和。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/02/22/ex1drawio.png" style="width: 250px; height: 250px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [1,2,3,4,5]
|
||||
<b>输出:</b>8
|
||||
<strong>解释:</strong>
|
||||
上图展示了得到数组三角和的过程。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [5]
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>
|
||||
由于 nums 中只有一个元素,数组的三角和为这个元素自己。</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 9</code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>你需要从空字符串开始 <strong>构造</strong> 一个长度为 <code>n</code> 的字符串 <code>s</code> ,构造的过程为每次给当前字符串 <strong>前面</strong> 添加 <strong>一个</strong> 字符。构造过程中得到的所有字符串编号为 <code>1</code> 到 <code>n</code> ,其中长度为 <code>i</code> 的字符串编号为 <code>s<sub>i</sub></code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,<code>s = "abaca"</code> ,<code>s<sub>1</sub> == "a"</code> ,<code>s<sub>2</sub> == "ca"</code> ,<code>s<sub>3</sub> == "aca"</code> 依次类推。</li>
|
||||
</ul>
|
||||
|
||||
<p><code>s<sub>i</sub></code> 的 <strong>得分</strong> 为 <code>s<sub>i</sub></code> 和 <code>s<sub>n</sub></code> 的 <strong>最长公共前缀</strong> 的长度(注意 <code>s == s<sub>n</sub></code> )。</p>
|
||||
|
||||
<p>给你最终的字符串 <code>s</code> ,请你返回每一个<em> </em><code>s<sub>i</sub></code> 的 <strong>得分之和</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "babab"
|
||||
<b>输出:</b>9
|
||||
<b>解释:</b>
|
||||
s<sub>1</sub> == "b" ,最长公共前缀是 "b" ,得分为 1 。
|
||||
s<sub>2</sub> == "ab" ,没有公共前缀,得分为 0 。
|
||||
s<sub>3</sub> == "bab" ,最长公共前缀为 "bab" ,得分为 3 。
|
||||
s<sub>4</sub> == "abab" ,没有公共前缀,得分为 0 。
|
||||
s<sub>5</sub> == "babab" ,最长公共前缀为 "babab" ,得分为 5 。
|
||||
得分和为 1 + 0 + 3 + 0 + 5 = 9 ,所以我们返回 9 。</pre>
|
||||
|
||||
<p><strong>示例 2 :</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "azbazbzaz"
|
||||
<b>输出:</b>14
|
||||
<b>解释:</b>
|
||||
s<sub>2</sub> == "az" ,最长公共前缀为 "az" ,得分为 2 。
|
||||
s<sub>6</sub> == "azbzaz" ,最长公共前缀为 "azb" ,得分为 3 。
|
||||
s<sub>9</sub> == "azbazbzaz" ,最长公共前缀为 "azbazbzaz" ,得分为 9 。
|
||||
其他 s<sub>i</sub> 得分均为 0 。
|
||||
得分和为 2 + 3 + 9 = 14 ,所以我们返回 14 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@ -0,0 +1,33 @@
|
||||
<p>给你一个 <strong>下标从 0 开始</strong> 的整数数组 <code>candies</code> 。数组中的每个元素表示大小为 <code>candies[i]</code> 的一堆糖果。你可以将每堆糖果分成任意数量的 <strong>子堆</strong> ,但 <strong>无法</strong> 再将两堆合并到一起。</p>
|
||||
|
||||
<p>另给你一个整数 <code>k</code> 。你需要将这些糖果分配给 <code>k</code> 个小孩,使每个小孩分到 <strong>相同</strong> 数量的糖果。每个小孩可以拿走 <strong>至多一堆</strong> 糖果,有些糖果可能会不被分配。</p>
|
||||
|
||||
<p>返回每个小孩可以拿走的 <strong>最大糖果数目</strong><em> </em>。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>candies = [5,8,6], k = 3
|
||||
<strong>输出:</strong>5
|
||||
<strong>解释:</strong>可以将 candies[1] 分成大小分别为 5 和 3 的两堆,然后把 candies[2] 分成大小分别为 5 和 1 的两堆。现在就有五堆大小分别为 5、5、3、5 和 1 的糖果。可以把 3 堆大小为 5 的糖果分给 3 个小孩。可以证明无法让每个小孩得到超过 5 颗糖果。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>candies = [2,5], k = 11
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>总共有 11 个小孩,但只有 7 颗糖果,但如果要分配糖果的话,必须保证每个小孩至少能得到 1 颗糖果。因此,最后每个小孩都没有得到糖果,答案是 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= candies.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= candies[i] <= 10<sup>7</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>12</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,36 @@
|
||||
<p>给你两个字符串 <code>current</code> 和 <code>correct</code> ,表示两个 <strong>24 小时制时间</strong> 。</p>
|
||||
|
||||
<p><strong>24 小时制时间</strong> 按 <code>"HH:MM"</code> 进行格式化,其中 <code>HH</code> 在 <code>00</code> 和 <code>23</code> 之间,而 <code>MM</code> 在 <code>00</code> 和 <code>59</code> 之间。最早的 24 小时制时间为 <code>00:00</code> ,最晚的是 <code>23:59</code> 。</p>
|
||||
|
||||
<p>在一步操作中,你可以将 <code>current</code> 这个时间增加 <code>1</code>、<code>5</code>、<code>15</code> 或 <code>60</code> 分钟。你可以执行这一操作 <strong>任意</strong> 次数。</p>
|
||||
|
||||
<p>返回将 <code>current</code><em> </em>转化为<em> </em><code>correct</code> 需要的 <strong>最少操作数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>current = "02:30", correct = "04:35"
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:
|
||||
</strong>可以按下述 3 步操作将 current 转换为 correct :
|
||||
- 为 current 加 60 分钟,current 变为 "03:30" 。
|
||||
- 为 current 加 60 分钟,current 变为 "04:30" 。
|
||||
- 为 current 加 5 分钟,current 变为 "04:35" 。
|
||||
可以证明,无法用少于 3 步操作将 current 转化为 correct 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>current = "11:00", correct = "11:01"
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>只需要为 current 加一分钟,所以最小操作数是 1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>current</code> 和 <code>correct</code> 都符合 <code>"HH:MM"</code> 格式</li>
|
||||
<li><code>current <= correct</code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>一次 <strong>位翻转</strong> 定义为将数字 <code>x</code> 二进制中的一个位进行 <strong>翻转</strong> 操作,即将 <code>0</code> 变成 <code>1</code> ,或者将 <code>1</code> 变成 <code>0</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,<code>x = 7</code> ,二进制表示为 <code>111</code> ,我们可以选择任意一个位(包含没有显示的前导 0 )并进行翻转。比方说我们可以翻转最右边一位得到 <code>110</code> ,或者翻转右边起第二位得到 <code>101</code> ,或者翻转右边起第五位(这一位是前导 0 )得到 <code>10111</code> 等等。</li>
|
||||
</ul>
|
||||
|
||||
<p>给你两个整数 <code>start</code> 和 <code>goal</code> ,请你返回将 <code>start</code> 转变成 <code>goal</code> 的 <strong>最少位翻转</strong> 次数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>start = 10, goal = 7
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>10 和 7 的二进制表示分别为 1010 和 0111 。我们可以通过 3 步将 10 转变成 7 :
|
||||
- 翻转右边起第一位得到:101<strong><em>0</em></strong> -> 101<strong><em>1 。</em></strong>
|
||||
- 翻转右边起第三位:1<strong><em>0</em></strong>11 -> 1<strong><em>1</em></strong>11 。
|
||||
- 翻转右边起第四位:<strong><em>1</em></strong>111 -> <strong><em>0</em></strong>111 。
|
||||
我们无法在 3 步内将 10 转变成 7 。所以我们返回 3 。</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>start = 3, goal = 4
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>3 和 4 的二进制表示分别为 011 和 100 。我们可以通过 3 步将 3 转变成 4 :
|
||||
- 翻转右边起第一位:01<strong><em>1</em></strong> -> 01<em><strong>0 </strong></em>。
|
||||
- 翻转右边起第二位:0<strong><em>1</em></strong>0 -> 0<strong><em>0</em></strong>0 。
|
||||
- 翻转右边起第三位:<strong><em>0</em></strong>00 -> <strong><em>1</em></strong>00 。
|
||||
我们无法在 3 步内将 3 变成 4 。所以我们返回 3 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= start, goal <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的二进制字符串 <code>s</code> ,它表示一条街沿途的建筑类型,其中:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>s[i] = '0'</code> 表示第 <code>i</code> 栋建筑是一栋办公楼,</li>
|
||||
<li><code>s[i] = '1'</code> 表示第 <code>i</code> 栋建筑是一间餐厅。</li>
|
||||
</ul>
|
||||
|
||||
<p>作为市政厅的官员,你需要随机<strong> 选择</strong> 3 栋建筑。然而,为了确保多样性,选出来的 3 栋建筑 <strong>相邻</strong> 的两栋不能是同一类型。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,给你 <code>s = "0<em><strong>0</strong></em>1<em><strong>1</strong></em>0<em><strong>1</strong></em>"</code> ,我们不能选择第 <code>1</code> ,<code>3</code> 和 <code>5</code> 栋建筑,因为得到的子序列是 <code>"0<em><strong>11</strong></em>"</code> ,有相邻两栋建筑是同一类型,所以 <strong>不合</strong> 题意。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回可以选择 3 栋建筑的 <strong>有效方案数</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "001101"
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>
|
||||
以下下标集合是合法的:
|
||||
- [0,2,4] ,从 "<em><strong>0</strong></em>0<em><strong>1</strong></em>1<em><strong>0</strong></em>1" 得到 "010"
|
||||
- [0,3,4] ,从 "<em><strong>0</strong></em>01<em><strong>10</strong></em>1" 得到 "010"
|
||||
- [1,2,4] ,从 "0<em><strong>01</strong></em>1<em><strong>0</strong></em>1" 得到 "010"
|
||||
- [1,3,4] ,从 "0<em><strong>0</strong></em>1<em><strong>10</strong></em>1" 得到 "010"
|
||||
- [2,4,5] ,从 "00<em><strong>1</strong></em>1<em><strong>01</strong></em>" 得到 "101"
|
||||
- [3,4,5] ,从 "001<em><strong>101</strong></em>" 得到 "101"
|
||||
没有别的合法选择,所以总共有 6 种方法。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>s = "11100"
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>没有任何符合题意的选择。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> 要么是 <code>'0'</code> ,要么是 <code>'1'</code> 。</li>
|
||||
</ul>
|
@ -0,0 +1,60 @@
|
||||
<p>You are given a character array <code>keys</code> containing <strong>unique</strong> characters and a string array <code>values</code> containing strings of length 2. You are also given another string array <code>dictionary</code> that contains all permitted original strings after decryption. You should implement a data structure that can encrypt or decrypt a <strong>0-indexed</strong> string.</p>
|
||||
|
||||
<p>A string is <strong>encrypted</strong> with the following process:</p>
|
||||
|
||||
<ol>
|
||||
<li>For each character <code>c</code> in the string, we find the index <code>i</code> satisfying <code>keys[i] == c</code> in <code>keys</code>.</li>
|
||||
<li>Replace <code>c</code> with <code>values[i]</code> in the string.</li>
|
||||
</ol>
|
||||
|
||||
<p>A string is <strong>decrypted</strong> with the following process:</p>
|
||||
|
||||
<ol>
|
||||
<li>For each substring <code>s</code> of length 2 occurring at an even index in the string, we find an <code>i</code> such that <code>values[i] == s</code>. If there are multiple valid <code>i</code>, we choose <strong>any</strong> one of them. This means a string could have multiple possible strings it can decrypt to.</li>
|
||||
<li>Replace <code>s</code> with <code>keys[i]</code> in the string.</li>
|
||||
</ol>
|
||||
|
||||
<p>Implement the <code>Encrypter</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>Encrypter(char[] keys, String[] values, String[] dictionary)</code> Initializes the <code>Encrypter</code> class with <code>keys, values</code>, and <code>dictionary</code>.</li>
|
||||
<li><code>String encrypt(String word1)</code> Encrypts <code>word1</code> with the encryption process described above and returns the encrypted string.</li>
|
||||
<li><code>int decrypt(String word2)</code> Returns the number of possible strings <code>word2</code> could decrypt to that also appear in <code>dictionary</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["Encrypter", "encrypt", "decrypt"]
|
||||
[[['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]], ["abcd"], ["eizfeiam"]]
|
||||
<strong>Output</strong>
|
||||
[null, "eizfeiam", 2]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
Encrypter encrypter = new Encrypter([['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]);
|
||||
encrypter.encrypt("abcd"); // return "eizfeiam".
|
||||
// 'a' maps to "ei", 'b' maps to "zf", 'c' maps to "ei", and 'd' maps to "am".
|
||||
encrypter.decrypt("eizfeiam"); // return 2.
|
||||
// "ei" can map to 'a' or 'c', "zf" maps to 'b', and "am" maps to 'd'.
|
||||
// Thus, the possible strings after decryption are "abad", "cbad", "abcd", and "cbcd".
|
||||
// 2 of those strings, "abad" and "abcd", appear in dictionary, so the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= keys.length == values.length <= 26</code></li>
|
||||
<li><code>values[i].length == 2</code></li>
|
||||
<li><code>1 <= dictionary.length <= 100</code></li>
|
||||
<li><code>1 <= dictionary[i].length <= 100</code></li>
|
||||
<li>All <code>keys[i]</code> and <code>dictionary[i]</code> are <strong>unique</strong>.</li>
|
||||
<li><code>1 <= word1.length <= 2000</code></li>
|
||||
<li><code>1 <= word2.length <= 200</code></li>
|
||||
<li>All <code>word1[i]</code> appear in <code>keys</code>.</li>
|
||||
<li><code>word2.length</code> is even.</li>
|
||||
<li><code>keys</code>, <code>values[i]</code>, <code>dictionary[i]</code>, <code>word1</code>, and <code>word2</code> only contain lowercase English letters.</li>
|
||||
<li>At most <code>200</code> calls will be made to <code>encrypt</code> and <code>decrypt</code> <strong>in total</strong>.</li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>You are given an integer array <code>matches</code> where <code>matches[i] = [winner<sub>i</sub>, loser<sub>i</sub>]</code> indicates that the player <code>winner<sub>i</sub></code> defeated player <code>loser<sub>i</sub></code> in a match.</p>
|
||||
|
||||
<p>Return <em>a list </em><code>answer</code><em> of size </em><code>2</code><em> where:</em></p>
|
||||
|
||||
<ul>
|
||||
<li><code>answer[0]</code> is a list of all players that have <strong>not</strong> lost any matches.</li>
|
||||
<li><code>answer[1]</code> is a list of all players that have lost exactly <strong>one</strong> match.</li>
|
||||
</ul>
|
||||
|
||||
<p>The values in the two lists should be returned in <strong>increasing</strong> order.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>You should only consider the players that have played <strong>at least one</strong> match.</li>
|
||||
<li>The testcases will be generated such that <strong>no</strong> two matches will have the <strong>same</strong> outcome.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
|
||||
<strong>Output:</strong> [[1,2,10],[4,5,7,8]]
|
||||
<strong>Explanation:</strong>
|
||||
Players 1, 2, and 10 have not lost any matches.
|
||||
Players 4, 5, 7, and 8 each have lost one match.
|
||||
Players 3, 6, and 9 each have lost two matches.
|
||||
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> matches = [[2,3],[1,3],[5,4],[6,4]]
|
||||
<strong>Output:</strong> [[1,2,5,6],[]]
|
||||
<strong>Explanation:</strong>
|
||||
Players 1, 2, 5, and 6 have not lost any matches.
|
||||
Players 3 and 4 each have lost two matches.
|
||||
Thus, answer[0] = [1,2,5,6] and answer[1] = [].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= matches.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>matches[i].length == 2</code></li>
|
||||
<li><code>1 <= winner<sub>i</sub>, loser<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
<li><code>winner<sub>i</sub> != loser<sub>i</sub></code></li>
|
||||
<li>All <code>matches[i]</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
@ -0,0 +1,37 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, where <code>nums[i]</code> is a digit between <code>0</code> and <code>9</code> (<strong>inclusive</strong>).</p>
|
||||
|
||||
<p>The <strong>triangular sum</strong> of <code>nums</code> is the value of the only element present in <code>nums</code> after the following process terminates:</p>
|
||||
|
||||
<ol>
|
||||
<li>Let <code>nums</code> comprise of <code>n</code> elements. If <code>n == 1</code>, <strong>end</strong> the process. Otherwise, <strong>create</strong> a new <strong>0-indexed</strong> integer array <code>newNums</code> of length <code>n - 1</code>.</li>
|
||||
<li>For each index <code>i</code>, where <code>0 <= i < n - 1</code>, <strong>assign</strong> the value of <code>newNums[i]</code> as <code>(nums[i] + nums[i+1]) % 10</code>, where <code>%</code> denotes modulo operator.</li>
|
||||
<li><strong>Replace</strong> the array <code>nums</code> with <code>newNums</code>.</li>
|
||||
<li><strong>Repeat</strong> the entire process starting from step 1.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the triangular sum of</em> <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/22/ex1drawio.png" style="width: 250px; height: 250px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
The above diagram depicts the process from which we obtain the triangular sum of the array.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
Since there is only one element in nums, the triangular sum is the value of that element itself.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 9</code></li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>You are <strong>building</strong> a string <code>s</code> of length <code>n</code> <strong>one</strong> character at a time, <strong>prepending</strong> each new character to the <strong>front</strong> of the string. The strings are labeled from <code>1</code> to <code>n</code>, where the string with length <code>i</code> is labeled <code>s<sub>i</sub></code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for <code>s = "abaca"</code>, <code>s<sub>1</sub> == "a"</code>, <code>s<sub>2</sub> == "ca"</code>, <code>s<sub>3</sub> == "aca"</code>, etc.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>score</strong> of <code>s<sub>i</sub></code> is the length of the <strong>longest common prefix</strong> between <code>s<sub>i</sub></code> and <code>s<sub>n</sub></code> (Note that <code>s == s<sub>n</sub></code>).</p>
|
||||
|
||||
<p>Given the final string <code>s</code>, return<em> the <strong>sum</strong> of the <strong>score</strong> of every </em><code>s<sub>i</sub></code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "babab"
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong>
|
||||
For s<sub>1</sub> == "b", the longest common prefix is "b" which has a score of 1.
|
||||
For s<sub>2</sub> == "ab", there is no common prefix so the score is 0.
|
||||
For s<sub>3</sub> == "bab", the longest common prefix is "bab" which has a score of 3.
|
||||
For s<sub>4</sub> == "abab", there is no common prefix so the score is 0.
|
||||
For s<sub>5</sub> == "babab", the longest common prefix is "babab" which has a score of 5.
|
||||
The sum of the scores is 1 + 0 + 3 + 0 + 5 = 9, so we return 9.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "azbazbzaz"
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong>
|
||||
For s<sub>2</sub> == "az", the longest common prefix is "az" which has a score of 2.
|
||||
For s<sub>6</sub> == "azbzaz", the longest common prefix is "azb" which has a score of 3.
|
||||
For s<sub>9</sub> == "azbazbzaz", the longest common prefix is "azbazbzaz" which has a score of 9.
|
||||
For all other s<sub>i</sub>, the score is 0.
|
||||
The sum of the scores is 2 + 3 + 9 = 14, so we return 14.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,30 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>candies</code>. Each element in the array denotes a pile of candies of size <code>candies[i]</code>. You can divide each pile into any number of <strong>sub piles</strong>, but you <strong>cannot</strong> merge two piles together.</p>
|
||||
|
||||
<p>You are also given an integer <code>k</code>. You should allocate piles of candies to <code>k</code> children such that each child gets the <strong>same</strong> number of candies. Each child can take <strong>at most one</strong> pile of candies and some piles of candies may go unused.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum number of candies</strong> each child can get.</em></p>
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candies = [5,8,6], k = 3
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candies = [2,5], k = 11
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= candies.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= candies[i] <= 10<sup>7</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>12</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,36 @@
|
||||
<p>You are given two strings <code>current</code> and <code>correct</code> representing two <strong>24-hour times</strong>.</p>
|
||||
|
||||
<p>24-hour times are formatted as <code>"HH:MM"</code>, where <code>HH</code> is between <code>00</code> and <code>23</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 24-hour time is <code>00:00</code>, and the latest is <code>23:59</code>.</p>
|
||||
|
||||
<p>In one operation you can increase the time <code>current</code> by <code>1</code>, <code>5</code>, <code>15</code>, or <code>60</code> minutes. You can perform this operation <strong>any</strong> number of times.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum number of operations</strong> needed to convert </em><code>current</code><em> to </em><code>correct</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> current = "02:30", correct = "04:35"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:
|
||||
</strong>We can convert current to correct in 3 operations as follows:
|
||||
- Add 60 minutes to current. current becomes "03:30".
|
||||
- Add 60 minutes to current. current becomes "04:30".
|
||||
- Add 5 minutes to current. current becomes "04:35".
|
||||
It can be proven that it is not possible to convert current to correct in fewer than 3 operations.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> current = "11:00", correct = "11:01"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We only have to add one minute to current, so the minimum number of operations needed is 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>current</code> and <code>correct</code> are in the format <code>"HH:MM"</code></li>
|
||||
<li><code>current <= correct</code></li>
|
||||
</ul>
|
@ -0,0 +1,38 @@
|
||||
<p>A <strong>bit flip</strong> of a number <code>x</code> is choosing a bit in the binary representation of <code>x</code> and <strong>flipping</strong> it from either <code>0</code> to <code>1</code> or <code>1</code> to <code>0</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for <code>x = 7</code>, the binary representation is <code>111</code> and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get <code>110</code>, flip the second bit from the right to get <code>101</code>, flip the fifth bit from the right (a leading zero) to get <code>10111</code>, etc.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given two integers <code>start</code> and <code>goal</code>, return<em> the <strong>minimum</strong> number of <strong>bit flips</strong> to convert </em><code>start</code><em> to </em><code>goal</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = 10, goal = 7
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
|
||||
- Flip the first bit from the right: 101<u>0</u> -> 101<u>1</u>.
|
||||
- Flip the third bit from the right: 1<u>0</u>11 -> 1<u>1</u>11.
|
||||
- Flip the fourth bit from the right: <u>1</u>111 -> <u>0</u>111.
|
||||
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = 3, goal = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
|
||||
- Flip the first bit from the right: 01<u>1</u> -> 01<u>0</u>.
|
||||
- Flip the second bit from the right: 0<u>1</u>0 -> 0<u>0</u>0.
|
||||
- Flip the third bit from the right: <u>0</u>00 -> <u>1</u>00.
|
||||
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= start, goal <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> which represents the types of buildings along a street where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>s[i] = '0'</code> denotes that the <code>i<sup>th</sup></code> building is an office and</li>
|
||||
<li><code>s[i] = '1'</code> denotes that the <code>i<sup>th</sup></code> building is a restaurant.</li>
|
||||
</ul>
|
||||
|
||||
<p>As a city official, you would like to <strong>select</strong> 3 buildings for random inspection. However, to ensure variety, <strong>no two consecutive</strong> buildings out of the <strong>selected</strong> buildings can be of the same type.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, given <code>s = "0<u><strong>0</strong></u>1<u><strong>1</strong></u>0<u><strong>1</strong></u>"</code>, we cannot select the <code>1<sup>st</sup></code>, <code>3<sup>rd</sup></code>, and <code>5<sup>th</sup></code> buildings as that would form <code>"0<strong><u>11</u></strong>"</code> which is <strong>not</strong> allowed due to having two consecutive buildings of the same type.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <b>number of valid ways</b> to select 3 buildings.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "001101"
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
The following sets of indices selected are valid:
|
||||
- [0,2,4] from "<u><strong>0</strong></u>0<strong><u>1</u></strong>1<strong><u>0</u></strong>1" forms "010"
|
||||
- [0,3,4] from "<u><strong>0</strong></u>01<u><strong>10</strong></u>1" forms "010"
|
||||
- [1,2,4] from "0<u><strong>01</strong></u>1<u><strong>0</strong></u>1" forms "010"
|
||||
- [1,3,4] from "0<u><strong>0</strong></u>1<u><strong>10</strong></u>1" forms "010"
|
||||
- [2,4,5] from "00<u><strong>1</strong></u>1<u><strong>01</strong></u>" forms "101"
|
||||
- [3,4,5] from "001<u><strong>101</strong></u>" forms "101"
|
||||
No other selection is valid. Thus, there are 6 total ways.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "11100"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> It can be shown that there are no valid selections.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
168
leetcode/originData/encrypt-and-decrypt-strings.json
Normal file
168
leetcode/originData/encrypt-and-decrypt-strings.json
Normal file
File diff suppressed because one or more lines are too long
161
leetcode/originData/find-players-with-zero-or-one-losses.json
Normal file
161
leetcode/originData/find-players-with-zero-or-one-losses.json
Normal file
File diff suppressed because one or more lines are too long
155
leetcode/originData/find-triangular-sum-of-an-array.json
Normal file
155
leetcode/originData/find-triangular-sum-of-an-array.json
Normal file
File diff suppressed because one or more lines are too long
168
leetcode/originData/maximum-candies-allocated-to-k-children.json
Normal file
168
leetcode/originData/maximum-candies-allocated-to-k-children.json
Normal file
File diff suppressed because one or more lines are too long
155
leetcode/originData/minimum-bit-flips-to-convert-number.json
Normal file
155
leetcode/originData/minimum-bit-flips-to-convert-number.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
157
leetcode/originData/number-of-ways-to-select-buildings.json
Normal file
157
leetcode/originData/number-of-ways-to-select-buildings.json
Normal file
File diff suppressed because one or more lines are too long
155
leetcode/originData/sum-of-scores-of-built-strings.json
Normal file
155
leetcode/originData/sum-of-scores-of-built-strings.json
Normal file
File diff suppressed because one or more lines are too long
60
leetcode/problem/encrypt-and-decrypt-strings.html
Normal file
60
leetcode/problem/encrypt-and-decrypt-strings.html
Normal file
@ -0,0 +1,60 @@
|
||||
<p>You are given a character array <code>keys</code> containing <strong>unique</strong> characters and a string array <code>values</code> containing strings of length 2. You are also given another string array <code>dictionary</code> that contains all permitted original strings after decryption. You should implement a data structure that can encrypt or decrypt a <strong>0-indexed</strong> string.</p>
|
||||
|
||||
<p>A string is <strong>encrypted</strong> with the following process:</p>
|
||||
|
||||
<ol>
|
||||
<li>For each character <code>c</code> in the string, we find the index <code>i</code> satisfying <code>keys[i] == c</code> in <code>keys</code>.</li>
|
||||
<li>Replace <code>c</code> with <code>values[i]</code> in the string.</li>
|
||||
</ol>
|
||||
|
||||
<p>A string is <strong>decrypted</strong> with the following process:</p>
|
||||
|
||||
<ol>
|
||||
<li>For each substring <code>s</code> of length 2 occurring at an even index in the string, we find an <code>i</code> such that <code>values[i] == s</code>. If there are multiple valid <code>i</code>, we choose <strong>any</strong> one of them. This means a string could have multiple possible strings it can decrypt to.</li>
|
||||
<li>Replace <code>s</code> with <code>keys[i]</code> in the string.</li>
|
||||
</ol>
|
||||
|
||||
<p>Implement the <code>Encrypter</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>Encrypter(char[] keys, String[] values, String[] dictionary)</code> Initializes the <code>Encrypter</code> class with <code>keys, values</code>, and <code>dictionary</code>.</li>
|
||||
<li><code>String encrypt(String word1)</code> Encrypts <code>word1</code> with the encryption process described above and returns the encrypted string.</li>
|
||||
<li><code>int decrypt(String word2)</code> Returns the number of possible strings <code>word2</code> could decrypt to that also appear in <code>dictionary</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["Encrypter", "encrypt", "decrypt"]
|
||||
[[['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]], ["abcd"], ["eizfeiam"]]
|
||||
<strong>Output</strong>
|
||||
[null, "eizfeiam", 2]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
Encrypter encrypter = new Encrypter([['a', 'b', 'c', 'd'], ["ei", "zf", "ei", "am"], ["abcd", "acbd", "adbc", "badc", "dacb", "cadb", "cbda", "abad"]);
|
||||
encrypter.encrypt("abcd"); // return "eizfeiam".
|
||||
// 'a' maps to "ei", 'b' maps to "zf", 'c' maps to "ei", and 'd' maps to "am".
|
||||
encrypter.decrypt("eizfeiam"); // return 2.
|
||||
// "ei" can map to 'a' or 'c', "zf" maps to 'b', and "am" maps to 'd'.
|
||||
// Thus, the possible strings after decryption are "abad", "cbad", "abcd", and "cbcd".
|
||||
// 2 of those strings, "abad" and "abcd", appear in dictionary, so the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= keys.length == values.length <= 26</code></li>
|
||||
<li><code>values[i].length == 2</code></li>
|
||||
<li><code>1 <= dictionary.length <= 100</code></li>
|
||||
<li><code>1 <= dictionary[i].length <= 100</code></li>
|
||||
<li>All <code>keys[i]</code> and <code>dictionary[i]</code> are <strong>unique</strong>.</li>
|
||||
<li><code>1 <= word1.length <= 2000</code></li>
|
||||
<li><code>1 <= word2.length <= 200</code></li>
|
||||
<li>All <code>word1[i]</code> appear in <code>keys</code>.</li>
|
||||
<li><code>word2.length</code> is even.</li>
|
||||
<li><code>keys</code>, <code>values[i]</code>, <code>dictionary[i]</code>, <code>word1</code>, and <code>word2</code> only contain lowercase English letters.</li>
|
||||
<li>At most <code>200</code> calls will be made to <code>encrypt</code> and <code>decrypt</code> <strong>in total</strong>.</li>
|
||||
</ul>
|
52
leetcode/problem/find-players-with-zero-or-one-losses.html
Normal file
52
leetcode/problem/find-players-with-zero-or-one-losses.html
Normal file
@ -0,0 +1,52 @@
|
||||
<p>You are given an integer array <code>matches</code> where <code>matches[i] = [winner<sub>i</sub>, loser<sub>i</sub>]</code> indicates that the player <code>winner<sub>i</sub></code> defeated player <code>loser<sub>i</sub></code> in a match.</p>
|
||||
|
||||
<p>Return <em>a list </em><code>answer</code><em> of size </em><code>2</code><em> where:</em></p>
|
||||
|
||||
<ul>
|
||||
<li><code>answer[0]</code> is a list of all players that have <strong>not</strong> lost any matches.</li>
|
||||
<li><code>answer[1]</code> is a list of all players that have lost exactly <strong>one</strong> match.</li>
|
||||
</ul>
|
||||
|
||||
<p>The values in the two lists should be returned in <strong>increasing</strong> order.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>You should only consider the players that have played <strong>at least one</strong> match.</li>
|
||||
<li>The testcases will be generated such that <strong>no</strong> two matches will have the <strong>same</strong> outcome.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> matches = [[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
|
||||
<strong>Output:</strong> [[1,2,10],[4,5,7,8]]
|
||||
<strong>Explanation:</strong>
|
||||
Players 1, 2, and 10 have not lost any matches.
|
||||
Players 4, 5, 7, and 8 each have lost one match.
|
||||
Players 3, 6, and 9 each have lost two matches.
|
||||
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> matches = [[2,3],[1,3],[5,4],[6,4]]
|
||||
<strong>Output:</strong> [[1,2,5,6],[]]
|
||||
<strong>Explanation:</strong>
|
||||
Players 1, 2, 5, and 6 have not lost any matches.
|
||||
Players 3 and 4 each have lost two matches.
|
||||
Thus, answer[0] = [1,2,5,6] and answer[1] = [].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= matches.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>matches[i].length == 2</code></li>
|
||||
<li><code>1 <= winner<sub>i</sub>, loser<sub>i</sub> <= 10<sup>5</sup></code></li>
|
||||
<li><code>winner<sub>i</sub> != loser<sub>i</sub></code></li>
|
||||
<li>All <code>matches[i]</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
37
leetcode/problem/find-triangular-sum-of-an-array.html
Normal file
37
leetcode/problem/find-triangular-sum-of-an-array.html
Normal file
@ -0,0 +1,37 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>, where <code>nums[i]</code> is a digit between <code>0</code> and <code>9</code> (<strong>inclusive</strong>).</p>
|
||||
|
||||
<p>The <strong>triangular sum</strong> of <code>nums</code> is the value of the only element present in <code>nums</code> after the following process terminates:</p>
|
||||
|
||||
<ol>
|
||||
<li>Let <code>nums</code> comprise of <code>n</code> elements. If <code>n == 1</code>, <strong>end</strong> the process. Otherwise, <strong>create</strong> a new <strong>0-indexed</strong> integer array <code>newNums</code> of length <code>n - 1</code>.</li>
|
||||
<li>For each index <code>i</code>, where <code>0 <= i < n - 1</code>, <strong>assign</strong> the value of <code>newNums[i]</code> as <code>(nums[i] + nums[i+1]) % 10</code>, where <code>%</code> denotes modulo operator.</li>
|
||||
<li><strong>Replace</strong> the array <code>nums</code> with <code>newNums</code>.</li>
|
||||
<li><strong>Repeat</strong> the entire process starting from step 1.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>the triangular sum of</em> <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/02/22/ex1drawio.png" style="width: 250px; height: 250px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4,5]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
The above diagram depicts the process from which we obtain the triangular sum of the array.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
Since there is only one element in nums, the triangular sum is the value of that element itself.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>0 <= nums[i] <= 9</code></li>
|
||||
</ul>
|
@ -0,0 +1,30 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>candies</code>. Each element in the array denotes a pile of candies of size <code>candies[i]</code>. You can divide each pile into any number of <strong>sub piles</strong>, but you <strong>cannot</strong> merge two piles together.</p>
|
||||
|
||||
<p>You are also given an integer <code>k</code>. You should allocate piles of candies to <code>k</code> children such that each child gets the <strong>same</strong> number of candies. Each child can take <strong>at most one</strong> pile of candies and some piles of candies may go unused.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum number of candies</strong> each child can get.</em></p>
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candies = [5,8,6], k = 3
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> We can divide candies[1] into 2 piles of size 5 and 3, and candies[2] into 2 piles of size 5 and 1. We now have five piles of candies of sizes 5, 5, 3, 5, and 1. We can allocate the 3 piles of size 5 to 3 children. It can be proven that each child cannot receive more than 5 candies.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> candies = [2,5], k = 11
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are 11 children but only 7 candies in total, so it is impossible to ensure each child receives at least one candy. Thus, each child gets no candy and the answer is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= candies.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= candies[i] <= 10<sup>7</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>12</sup></code></li>
|
||||
</ul>
|
38
leetcode/problem/minimum-bit-flips-to-convert-number.html
Normal file
38
leetcode/problem/minimum-bit-flips-to-convert-number.html
Normal file
@ -0,0 +1,38 @@
|
||||
<p>A <strong>bit flip</strong> of a number <code>x</code> is choosing a bit in the binary representation of <code>x</code> and <strong>flipping</strong> it from either <code>0</code> to <code>1</code> or <code>1</code> to <code>0</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for <code>x = 7</code>, the binary representation is <code>111</code> and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get <code>110</code>, flip the second bit from the right to get <code>101</code>, flip the fifth bit from the right (a leading zero) to get <code>10111</code>, etc.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given two integers <code>start</code> and <code>goal</code>, return<em> the <strong>minimum</strong> number of <strong>bit flips</strong> to convert </em><code>start</code><em> to </em><code>goal</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = 10, goal = 7
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
|
||||
- Flip the first bit from the right: 101<u>0</u> -> 101<u>1</u>.
|
||||
- Flip the third bit from the right: 1<u>0</u>11 -> 1<u>1</u>11.
|
||||
- Flip the fourth bit from the right: <u>1</u>111 -> <u>0</u>111.
|
||||
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> start = 3, goal = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
|
||||
- Flip the first bit from the right: 01<u>1</u> -> 01<u>0</u>.
|
||||
- Flip the second bit from the right: 0<u>1</u>0 -> 0<u>0</u>0.
|
||||
- Flip the third bit from the right: <u>0</u>00 -> <u>1</u>00.
|
||||
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= start, goal <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,36 @@
|
||||
<p>You are given two strings <code>current</code> and <code>correct</code> representing two <strong>24-hour times</strong>.</p>
|
||||
|
||||
<p>24-hour times are formatted as <code>"HH:MM"</code>, where <code>HH</code> is between <code>00</code> and <code>23</code>, and <code>MM</code> is between <code>00</code> and <code>59</code>. The earliest 24-hour time is <code>00:00</code>, and the latest is <code>23:59</code>.</p>
|
||||
|
||||
<p>In one operation you can increase the time <code>current</code> by <code>1</code>, <code>5</code>, <code>15</code>, or <code>60</code> minutes. You can perform this operation <strong>any</strong> number of times.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum number of operations</strong> needed to convert </em><code>current</code><em> to </em><code>correct</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> current = "02:30", correct = "04:35"
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:
|
||||
</strong>We can convert current to correct in 3 operations as follows:
|
||||
- Add 60 minutes to current. current becomes "03:30".
|
||||
- Add 60 minutes to current. current becomes "04:30".
|
||||
- Add 5 minutes to current. current becomes "04:35".
|
||||
It can be proven that it is not possible to convert current to correct in fewer than 3 operations.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> current = "11:00", correct = "11:01"
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We only have to add one minute to current, so the minimum number of operations needed is 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>current</code> and <code>correct</code> are in the format <code>"HH:MM"</code></li>
|
||||
<li><code>current <= correct</code></li>
|
||||
</ul>
|
47
leetcode/problem/number-of-ways-to-select-buildings.html
Normal file
47
leetcode/problem/number-of-ways-to-select-buildings.html
Normal file
@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> binary string <code>s</code> which represents the types of buildings along a street where:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>s[i] = '0'</code> denotes that the <code>i<sup>th</sup></code> building is an office and</li>
|
||||
<li><code>s[i] = '1'</code> denotes that the <code>i<sup>th</sup></code> building is a restaurant.</li>
|
||||
</ul>
|
||||
|
||||
<p>As a city official, you would like to <strong>select</strong> 3 buildings for random inspection. However, to ensure variety, <strong>no two consecutive</strong> buildings out of the <strong>selected</strong> buildings can be of the same type.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, given <code>s = "0<u><strong>0</strong></u>1<u><strong>1</strong></u>0<u><strong>1</strong></u>"</code>, we cannot select the <code>1<sup>st</sup></code>, <code>3<sup>rd</sup></code>, and <code>5<sup>th</sup></code> buildings as that would form <code>"0<strong><u>11</u></strong>"</code> which is <strong>not</strong> allowed due to having two consecutive buildings of the same type.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <b>number of valid ways</b> to select 3 buildings.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "001101"
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
The following sets of indices selected are valid:
|
||||
- [0,2,4] from "<u><strong>0</strong></u>0<strong><u>1</u></strong>1<strong><u>0</u></strong>1" forms "010"
|
||||
- [0,3,4] from "<u><strong>0</strong></u>01<u><strong>10</strong></u>1" forms "010"
|
||||
- [1,2,4] from "0<u><strong>01</strong></u>1<u><strong>0</strong></u>1" forms "010"
|
||||
- [1,3,4] from "0<u><strong>0</strong></u>1<u><strong>10</strong></u>1" forms "010"
|
||||
- [2,4,5] from "00<u><strong>1</strong></u>1<u><strong>01</strong></u>" forms "101"
|
||||
- [3,4,5] from "001<u><strong>101</strong></u>" forms "101"
|
||||
No other selection is valid. Thus, there are 6 total ways.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "11100"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> It can be shown that there are no valid selections.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
44
leetcode/problem/sum-of-scores-of-built-strings.html
Normal file
44
leetcode/problem/sum-of-scores-of-built-strings.html
Normal file
@ -0,0 +1,44 @@
|
||||
<p>You are <strong>building</strong> a string <code>s</code> of length <code>n</code> <strong>one</strong> character at a time, <strong>prepending</strong> each new character to the <strong>front</strong> of the string. The strings are labeled from <code>1</code> to <code>n</code>, where the string with length <code>i</code> is labeled <code>s<sub>i</sub></code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for <code>s = "abaca"</code>, <code>s<sub>1</sub> == "a"</code>, <code>s<sub>2</sub> == "ca"</code>, <code>s<sub>3</sub> == "aca"</code>, etc.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>score</strong> of <code>s<sub>i</sub></code> is the length of the <strong>longest common prefix</strong> between <code>s<sub>i</sub></code> and <code>s<sub>n</sub></code> (Note that <code>s == s<sub>n</sub></code>).</p>
|
||||
|
||||
<p>Given the final string <code>s</code>, return<em> the <strong>sum</strong> of the <strong>score</strong> of every </em><code>s<sub>i</sub></code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "babab"
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong>
|
||||
For s<sub>1</sub> == "b", the longest common prefix is "b" which has a score of 1.
|
||||
For s<sub>2</sub> == "ab", there is no common prefix so the score is 0.
|
||||
For s<sub>3</sub> == "bab", the longest common prefix is "bab" which has a score of 3.
|
||||
For s<sub>4</sub> == "abab", there is no common prefix so the score is 0.
|
||||
For s<sub>5</sub> == "babab", the longest common prefix is "babab" which has a score of 5.
|
||||
The sum of the scores is 1 + 0 + 3 + 0 + 5 = 9, so we return 9.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "azbazbzaz"
|
||||
<strong>Output:</strong> 14
|
||||
<strong>Explanation:</strong>
|
||||
For s<sub>2</sub> == "az", the longest common prefix is "az" which has a score of 2.
|
||||
For s<sub>6</sub> == "azbzaz", the longest common prefix is "azb" which has a score of 3.
|
||||
For s<sub>9</sub> == "azbazbzaz", the longest common prefix is "azbazbzaz" which has a score of 9.
|
||||
For all other s<sub>i</sub>, the score is 0.
|
||||
The sum of the scores is 2 + 3 + 9 = 14, so we return 14.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user