diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md index 58d325892108f..a8ca434436f2e 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md @@ -165,13 +165,13 @@ func letterCombinations(digits string) []string { ```ts function letterCombinations(digits: string): string[] { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans: string[] = ['']; const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; for (const i of digits) { - const s = d[parseInt(i) - 2]; + const s = d[+i - 2]; const t: string[] = []; for (const a of ans) { for (const b of s) { @@ -218,13 +218,13 @@ impl Solution { * @return {string[]} */ var letterCombinations = function (digits) { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans = ['']; const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; for (const i of digits) { - const s = d[parseInt(i) - 2]; + const s = d[+i - 2]; const t = []; for (const a of ans) { for (const b of s) { @@ -392,7 +392,7 @@ func letterCombinations(digits string) (ans []string) { ```ts function letterCombinations(digits: string): string[] { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans: string[] = []; @@ -403,7 +403,7 @@ function letterCombinations(digits: string): string[] { ans.push(t.join('')); return; } - const s = d[parseInt(digits[i]) - 2]; + const s = d[+digits[i] - 2]; for (const c of s) { t.push(c); dfs(i + 1); @@ -453,7 +453,7 @@ impl Solution { * @return {string[]} */ var letterCombinations = function (digits) { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans = []; @@ -464,7 +464,7 @@ var letterCombinations = function (digits) { ans.push(t.join('')); return; } - const s = d[parseInt(digits[i]) - 2]; + const s = d[+digits[i] - 2]; for (const c of s) { t.push(c); dfs(i + 1); diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md index a15369a5b20c5..ffad07737163e 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md @@ -161,13 +161,13 @@ func letterCombinations(digits string) []string { ```ts function letterCombinations(digits: string): string[] { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans: string[] = ['']; const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; for (const i of digits) { - const s = d[parseInt(i) - 2]; + const s = d[+i - 2]; const t: string[] = []; for (const a of ans) { for (const b of s) { @@ -214,13 +214,13 @@ impl Solution { * @return {string[]} */ var letterCombinations = function (digits) { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans = ['']; const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; for (const i of digits) { - const s = d[parseInt(i) - 2]; + const s = d[+i - 2]; const t = []; for (const a of ans) { for (const b of s) { @@ -388,7 +388,7 @@ func letterCombinations(digits string) (ans []string) { ```ts function letterCombinations(digits: string): string[] { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans: string[] = []; @@ -399,7 +399,7 @@ function letterCombinations(digits: string): string[] { ans.push(t.join('')); return; } - const s = d[parseInt(digits[i]) - 2]; + const s = d[+digits[i] - 2]; for (const c of s) { t.push(c); dfs(i + 1); @@ -449,7 +449,7 @@ impl Solution { * @return {string[]} */ var letterCombinations = function (digits) { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans = []; @@ -460,7 +460,7 @@ var letterCombinations = function (digits) { ans.push(t.join('')); return; } - const s = d[parseInt(digits[i]) - 2]; + const s = d[+digits[i] - 2]; for (const c of s) { t.push(c); dfs(i + 1); diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.js b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.js index 6b35a1b0bbf0b..e84f557ebd4ef 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.js +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.js @@ -3,13 +3,13 @@ * @return {string[]} */ var letterCombinations = function (digits) { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans = ['']; const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; for (const i of digits) { - const s = d[parseInt(i) - 2]; + const s = d[+i - 2]; const t = []; for (const a of ans) { for (const b of s) { diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.ts b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.ts index e879eb6a5972d..125c7081772f5 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.ts +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution.ts @@ -1,11 +1,11 @@ function letterCombinations(digits: string): string[] { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans: string[] = ['']; const d = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']; for (const i of digits) { - const s = d[parseInt(i) - 2]; + const s = d[+i - 2]; const t: string[] = []; for (const a of ans) { for (const b of s) { diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.js b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.js index 3ba4721ef1457..3a1fc00deb2d8 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.js +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.js @@ -3,7 +3,7 @@ * @return {string[]} */ var letterCombinations = function (digits) { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans = []; @@ -14,7 +14,7 @@ var letterCombinations = function (digits) { ans.push(t.join('')); return; } - const s = d[parseInt(digits[i]) - 2]; + const s = d[+digits[i] - 2]; for (const c of s) { t.push(c); dfs(i + 1); diff --git a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts index 3c6dce34cf09c..9bb182b02cc8c 100644 --- a/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts +++ b/solution/0000-0099/0017.Letter Combinations of a Phone Number/Solution2.ts @@ -1,5 +1,5 @@ function letterCombinations(digits: string): string[] { - if (digits.length == 0) { + if (digits.length === 0) { return []; } const ans: string[] = []; @@ -10,7 +10,7 @@ function letterCombinations(digits: string): string[] { ans.push(t.join('')); return; } - const s = d[parseInt(digits[i]) - 2]; + const s = d[+digits[i] - 2]; for (const c of s) { t.push(c); dfs(i + 1); diff --git a/solution/0700-0799/0724.Find Pivot Index/README.md b/solution/0700-0799/0724.Find Pivot Index/README.md index ab2819c3fa462..17574d5d4670c 100644 --- a/solution/0700-0799/0724.Find Pivot Index/README.md +++ b/solution/0700-0799/0724.Find Pivot Index/README.md @@ -77,13 +77,13 @@ tags: ### 方法一:前缀和 -我们定义变量 $left$ 表示数组 `nums` 中下标 $i$ 左侧元素之和,变量 $right$ 表示数组 `nums` 中下标 $i$ 右侧元素之和。初始时 $left = 0$, $right = \sum_{i = 0}^{n - 1} nums[i]$。 +我们定义变量 $left$ 表示数组 $\textit{nums}$ 中下标 $i$ 左侧元素之和,变量 $right$ 表示数组 $\textit{nums}$ 中下标 $i$ 右侧元素之和。初始时 $left = 0$, $right = \sum_{i = 0}^{n - 1} nums[i]$。 -遍历数组 `nums`,对于当前遍历到的数字 $x$,我们更新 $right = right - x$,此时如果 $left=right$,说明当前下标 $i$ 就是中间位置,直接返回即可。否则,我们更新 $left = left + x$,继续遍历下一个数字。 +遍历数组 $\textit{nums}$,对于当前遍历到的数字 $x$,我们更新 $right = right - x$,此时如果 $left=right$,说明当前下标 $i$ 就是中间位置,直接返回即可。否则,我们更新 $left = left + x$,继续遍历下一个数字。 遍历结束,如果没有找到中间位置,返回 $-1$。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。 相似题目: diff --git a/solution/0700-0799/0724.Find Pivot Index/README_EN.md b/solution/0700-0799/0724.Find Pivot Index/README_EN.md index e9fb09d3d9383..7bf93341a4b5f 100644 --- a/solution/0700-0799/0724.Find Pivot Index/README_EN.md +++ b/solution/0700-0799/0724.Find Pivot Index/README_EN.md @@ -73,7 +73,20 @@ Right sum = nums[1] + nums[2] = 1 + -1 = 0 -### Solution 1 +### Solution 1: Prefix Sum + +We define a variable $left$ to represent the sum of elements to the left of index $i$ in the array $\textit{nums}$, and a variable $right$ to represent the sum of elements to the right of index $i$ in the array $\textit{nums}$. Initially, $left = 0$, $right = \sum_{i = 0}^{n - 1} nums[i]$. + +We traverse the array $\textit{nums}$. For the current number $x$ being traversed, we update $right = right - x$. At this point, if $left = right$, it indicates that the current index $i$ is the middle position, and we can return it directly. Otherwise, we update $left = left + x$ and continue to traverse the next number. + +If the middle position is not found by the end of the traversal, return $-1$. + +The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $\textit{nums}$. + +Similar Problems: + +- [1991. Find the Middle Index in Array](https://github.com/doocs/leetcode/blob/main/solution/1900-1999/1991.Find%20the%20Middle%20Index%20in%20Array/README_EN.md) +- [2574. Left and Right Sum Differences](https://github.com/doocs/leetcode/blob/main/solution/2500-2599/2574.Left%20and%20Right%20Sum%20Differences/README_EN.md) diff --git a/solution/3200-3299/3210.Find the Encrypted String/README.md b/solution/3200-3299/3210.Find the Encrypted String/README.md index 494204c8da8dc..a5775a7640bef 100644 --- a/solution/3200-3299/3210.Find the Encrypted String/README.md +++ b/solution/3200-3299/3210.Find the Encrypted String/README.md @@ -69,32 +69,80 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3210.Fi -### 方法一 +### 方法一:模拟 + +我们可以使用模拟的方法,对字符串的第 $i$ 个字符,我们将其替换为字符串的第 $(i + k) \bmod n$ 个字符。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。 #### Python3 ```python - +class Solution: + def getEncryptedString(self, s: str, k: int) -> str: + cs = list(s) + n = len(s) + for i in range(n): + cs[i] = s[(i + k) % n] + return "".join(cs) ``` #### Java ```java - +class Solution { + public String getEncryptedString(String s, int k) { + char[] cs = s.toCharArray(); + int n = cs.length; + for (int i = 0; i < n; ++i) { + cs[i] = s.charAt((i + k) % n); + } + return new String(cs); + } +} ``` #### C++ ```cpp - +class Solution { +public: + string getEncryptedString(string s, int k) { + int n = s.length(); + string cs(n, ' '); + for (int i = 0; i < n; ++i) { + cs[i] = s[(i + k) % n]; + } + return cs; + } +}; ``` #### Go ```go +func getEncryptedString(s string, k int) string { + cs := []byte(s) + for i := range s { + cs[i] = s[(i+k)%len(s)] + } + return string(cs) +} +``` +#### TypeScript + +```ts +function getEncryptedString(s: string, k: number): string { + const cs: string[] = []; + const n = s.length; + for (let i = 0; i < n; ++i) { + cs[i] = s[(i + k) % n]; + } + return cs.join(''); +} ``` diff --git a/solution/3200-3299/3210.Find the Encrypted String/README_EN.md b/solution/3200-3299/3210.Find the Encrypted String/README_EN.md index 75fddbb24f0d7..d509f8421cdbb 100644 --- a/solution/3200-3299/3210.Find the Encrypted String/README_EN.md +++ b/solution/3200-3299/3210.Find the Encrypted String/README_EN.md @@ -67,32 +67,80 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3210.Fi -### Solution 1 +### Solution 1: Simulation + +We can use the simulation method. For the $i^{th}$ character of the string, we replace it with the character at position $(i + k) \bmod n$ of the string. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the string $s$. #### Python3 ```python - +class Solution: + def getEncryptedString(self, s: str, k: int) -> str: + cs = list(s) + n = len(s) + for i in range(n): + cs[i] = s[(i + k) % n] + return "".join(cs) ``` #### Java ```java - +class Solution { + public String getEncryptedString(String s, int k) { + char[] cs = s.toCharArray(); + int n = cs.length; + for (int i = 0; i < n; ++i) { + cs[i] = s.charAt((i + k) % n); + } + return new String(cs); + } +} ``` #### C++ ```cpp - +class Solution { +public: + string getEncryptedString(string s, int k) { + int n = s.length(); + string cs(n, ' '); + for (int i = 0; i < n; ++i) { + cs[i] = s[(i + k) % n]; + } + return cs; + } +}; ``` #### Go ```go +func getEncryptedString(s string, k int) string { + cs := []byte(s) + for i := range s { + cs[i] = s[(i+k)%len(s)] + } + return string(cs) +} +``` +#### TypeScript + +```ts +function getEncryptedString(s: string, k: number): string { + const cs: string[] = []; + const n = s.length; + for (let i = 0; i < n; ++i) { + cs[i] = s[(i + k) % n]; + } + return cs.join(''); +} ``` diff --git a/solution/3200-3299/3210.Find the Encrypted String/Solution.cpp b/solution/3200-3299/3210.Find the Encrypted String/Solution.cpp new file mode 100644 index 0000000000000..5cc00713beb32 --- /dev/null +++ b/solution/3200-3299/3210.Find the Encrypted String/Solution.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + string getEncryptedString(string s, int k) { + int n = s.length(); + string cs(n, ' '); + for (int i = 0; i < n; ++i) { + cs[i] = s[(i + k) % n]; + } + return cs; + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3210.Find the Encrypted String/Solution.go b/solution/3200-3299/3210.Find the Encrypted String/Solution.go new file mode 100644 index 0000000000000..ef7fcfd4e342d --- /dev/null +++ b/solution/3200-3299/3210.Find the Encrypted String/Solution.go @@ -0,0 +1,7 @@ +func getEncryptedString(s string, k int) string { + cs := []byte(s) + for i := range s { + cs[i] = s[(i+k)%len(s)] + } + return string(cs) +} \ No newline at end of file diff --git a/solution/3200-3299/3210.Find the Encrypted String/Solution.java b/solution/3200-3299/3210.Find the Encrypted String/Solution.java new file mode 100644 index 0000000000000..72a241e735051 --- /dev/null +++ b/solution/3200-3299/3210.Find the Encrypted String/Solution.java @@ -0,0 +1,10 @@ +class Solution { + public String getEncryptedString(String s, int k) { + char[] cs = s.toCharArray(); + int n = cs.length; + for (int i = 0; i < n; ++i) { + cs[i] = s.charAt((i + k) % n); + } + return new String(cs); + } +} \ No newline at end of file diff --git a/solution/3200-3299/3210.Find the Encrypted String/Solution.py b/solution/3200-3299/3210.Find the Encrypted String/Solution.py new file mode 100644 index 0000000000000..34f3bc75299e3 --- /dev/null +++ b/solution/3200-3299/3210.Find the Encrypted String/Solution.py @@ -0,0 +1,7 @@ +class Solution: + def getEncryptedString(self, s: str, k: int) -> str: + cs = list(s) + n = len(s) + for i in range(n): + cs[i] = s[(i + k) % n] + return "".join(cs) diff --git a/solution/3200-3299/3210.Find the Encrypted String/Solution.ts b/solution/3200-3299/3210.Find the Encrypted String/Solution.ts new file mode 100644 index 0000000000000..602ce2274005c --- /dev/null +++ b/solution/3200-3299/3210.Find the Encrypted String/Solution.ts @@ -0,0 +1,8 @@ +function getEncryptedString(s: string, k: number): string { + const cs: string[] = []; + const n = s.length; + for (let i = 0; i < n; ++i) { + cs[i] = s[(i + k) % n]; + } + return cs.join(''); +} diff --git a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README.md b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README.md index 093bc092da1e0..af96e053ad8ee 100644 --- a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README.md +++ b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README.md @@ -60,32 +60,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3211.Ge -### 方法一 +### 方法一:DFS + +我们可以枚举长度为 $n$ 的二进制字符串的每个位置 $i$,然后对于每个位置 $i$,我们可以枚举其可以取的值 $j$,如果 $j$ 为 $0$,那么我们需要判断其前一个位置是否为 $1$,如果为 $1$,则可以继续递归下去,否则不合法,如果 $j$ 为 $1$,则直接递归下去。 + +时间复杂度 $O(n \times 2^n)$,其中 $n$ 为字符串长度。忽略答案数组的空间消耗,空间复杂度 $O(n)$。 #### Python3 ```python - +class Solution: + def validStrings(self, n: int) -> List[str]: + def dfs(i: int): + if i >= n: + ans.append("".join(t)) + return + for j in range(2): + if (j == 0 and (i == 0 or t[i - 1] == "1")) or j == 1: + t.append(str(j)) + dfs(i + 1) + t.pop() + + ans = [] + t = [] + dfs(0) + return ans ``` #### Java ```java - +class Solution { + private List ans = new ArrayList<>(); + private StringBuilder t = new StringBuilder(); + private int n; + + public List validStrings(int n) { + this.n = n; + dfs(0); + return ans; + } + + private void dfs(int i) { + if (i >= n) { + ans.add(t.toString()); + return; + } + for (int j = 0; j < 2; ++j) { + if ((j == 0 && (i == 0 || t.charAt(i - 1) == '1')) || j == 1) { + t.append(j); + dfs(i + 1); + t.deleteCharAt(t.length() - 1); + } + } + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector validStrings(int n) { + vector ans; + string t; + auto dfs = [&](auto&& dfs, int i) { + if (i >= n) { + ans.emplace_back(t); + return; + } + for (int j = 0; j < 2; ++j) { + if ((j == 0 && (i == 0 || t[i - 1] == '1')) || j == 1) { + t.push_back('0' + j); + dfs(dfs, i + 1); + t.pop_back(); + } + } + }; + dfs(dfs, 0); + return ans; + } +}; ``` #### Go ```go +func validStrings(n int) (ans []string) { + t := []byte{} + var dfs func(int) + dfs = func(i int) { + if i >= n { + ans = append(ans, string(t)) + return + } + for j := 0; j < 2; j++ { + if (j == 0 && (i == 0 || t[i-1] == '1')) || j == 1 { + t = append(t, byte('0'+j)) + dfs(i + 1) + t = t[:len(t)-1] + } + } + } + dfs(0) + return +} +``` +#### TypeScript + +```ts +function validStrings(n: number): string[] { + const ans: string[] = []; + const t: string[] = []; + const dfs = (i: number) => { + if (i >= n) { + ans.push(t.join('')); + return; + } + for (let j = 0; j < 2; ++j) { + if ((j == 0 && (i == 0 || t[i - 1] == '1')) || j == 1) { + t.push(j.toString()); + dfs(i + 1); + t.pop(); + } + } + }; + dfs(0); + return ans; +} ``` diff --git a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README_EN.md b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README_EN.md index bd8bed5841647..4f8800aeabc54 100644 --- a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README_EN.md +++ b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/README_EN.md @@ -58,32 +58,138 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3211.Ge -### Solution 1 +### Solution 1: DFS + +We can enumerate each position $i$ of a binary string of length $n$, and for each position $i$, we can enumerate the possible value $j$ it can take. If $j$ is $0$, then we need to check if its previous position is $1$. If it is $1$, we can continue to recurse further; otherwise, it is invalid. If $j$ is $1$, then we directly recurse further. + +The time complexity is $O(n \times 2^n)$, where $n$ is the length of the string. Ignoring the space consumption of the answer array, the space complexity is $O(n)$. #### Python3 ```python - +class Solution: + def validStrings(self, n: int) -> List[str]: + def dfs(i: int): + if i >= n: + ans.append("".join(t)) + return + for j in range(2): + if (j == 0 and (i == 0 or t[i - 1] == "1")) or j == 1: + t.append(str(j)) + dfs(i + 1) + t.pop() + + ans = [] + t = [] + dfs(0) + return ans ``` #### Java ```java - +class Solution { + private List ans = new ArrayList<>(); + private StringBuilder t = new StringBuilder(); + private int n; + + public List validStrings(int n) { + this.n = n; + dfs(0); + return ans; + } + + private void dfs(int i) { + if (i >= n) { + ans.add(t.toString()); + return; + } + for (int j = 0; j < 2; ++j) { + if ((j == 0 && (i == 0 || t.charAt(i - 1) == '1')) || j == 1) { + t.append(j); + dfs(i + 1); + t.deleteCharAt(t.length() - 1); + } + } + } +} ``` #### C++ ```cpp - +class Solution { +public: + vector validStrings(int n) { + vector ans; + string t; + auto dfs = [&](auto&& dfs, int i) { + if (i >= n) { + ans.emplace_back(t); + return; + } + for (int j = 0; j < 2; ++j) { + if ((j == 0 && (i == 0 || t[i - 1] == '1')) || j == 1) { + t.push_back('0' + j); + dfs(dfs, i + 1); + t.pop_back(); + } + } + }; + dfs(dfs, 0); + return ans; + } +}; ``` #### Go ```go +func validStrings(n int) (ans []string) { + t := []byte{} + var dfs func(int) + dfs = func(i int) { + if i >= n { + ans = append(ans, string(t)) + return + } + for j := 0; j < 2; j++ { + if (j == 0 && (i == 0 || t[i-1] == '1')) || j == 1 { + t = append(t, byte('0'+j)) + dfs(i + 1) + t = t[:len(t)-1] + } + } + } + dfs(0) + return +} +``` +#### TypeScript + +```ts +function validStrings(n: number): string[] { + const ans: string[] = []; + const t: string[] = []; + const dfs = (i: number) => { + if (i >= n) { + ans.push(t.join('')); + return; + } + for (let j = 0; j < 2; ++j) { + if ((j == 0 && (i == 0 || t[i - 1] == '1')) || j == 1) { + t.push(j.toString()); + dfs(i + 1); + t.pop(); + } + } + }; + dfs(0); + return ans; +} ``` diff --git a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.cpp b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.cpp new file mode 100644 index 0000000000000..8b11f7530f266 --- /dev/null +++ b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + vector validStrings(int n) { + vector ans; + string t; + auto dfs = [&](auto&& dfs, int i) { + if (i >= n) { + ans.emplace_back(t); + return; + } + for (int j = 0; j < 2; ++j) { + if ((j == 0 && (i == 0 || t[i - 1] == '1')) || j == 1) { + t.push_back('0' + j); + dfs(dfs, i + 1); + t.pop_back(); + } + } + }; + dfs(dfs, 0); + return ans; + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.go b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.go new file mode 100644 index 0000000000000..755e22a671165 --- /dev/null +++ b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.go @@ -0,0 +1,19 @@ +func validStrings(n int) (ans []string) { + t := []byte{} + var dfs func(int) + dfs = func(i int) { + if i >= n { + ans = append(ans, string(t)) + return + } + for j := 0; j < 2; j++ { + if (j == 0 && (i == 0 || t[i-1] == '1')) || j == 1 { + t = append(t, byte('0'+j)) + dfs(i + 1) + t = t[:len(t)-1] + } + } + } + dfs(0) + return +} \ No newline at end of file diff --git a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.java b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.java new file mode 100644 index 0000000000000..1dcb927622ff9 --- /dev/null +++ b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.java @@ -0,0 +1,25 @@ +class Solution { + private List ans = new ArrayList<>(); + private StringBuilder t = new StringBuilder(); + private int n; + + public List validStrings(int n) { + this.n = n; + dfs(0); + return ans; + } + + private void dfs(int i) { + if (i >= n) { + ans.add(t.toString()); + return; + } + for (int j = 0; j < 2; ++j) { + if ((j == 0 && (i == 0 || t.charAt(i - 1) == '1')) || j == 1) { + t.append(j); + dfs(i + 1); + t.deleteCharAt(t.length() - 1); + } + } + } +} \ No newline at end of file diff --git a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.py b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.py new file mode 100644 index 0000000000000..d3fc01430f51f --- /dev/null +++ b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.py @@ -0,0 +1,16 @@ +class Solution: + def validStrings(self, n: int) -> List[str]: + def dfs(i: int): + if i >= n: + ans.append("".join(t)) + return + for j in range(2): + if (j == 0 and (i == 0 or t[i - 1] == "1")) or j == 1: + t.append(str(j)) + dfs(i + 1) + t.pop() + + ans = [] + t = [] + dfs(0) + return ans diff --git a/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.ts b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.ts new file mode 100644 index 0000000000000..eb765f7e9b6bc --- /dev/null +++ b/solution/3200-3299/3211.Generate Binary Strings Without Adjacent Zeros/Solution.ts @@ -0,0 +1,19 @@ +function validStrings(n: number): string[] { + const ans: string[] = []; + const t: string[] = []; + const dfs = (i: number) => { + if (i >= n) { + ans.push(t.join('')); + return; + } + for (let j = 0; j < 2; ++j) { + if ((j == 0 && (i == 0 || t[i - 1] == '1')) || j == 1) { + t.push(j.toString()); + dfs(i + 1); + t.pop(); + } + } + }; + dfs(0); + return ans; +} diff --git a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README.md b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README.md index bd2a0ad131dc8..1bf8be06470a4 100644 --- a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README.md +++ b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README.md @@ -75,32 +75,144 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3212.Co -### 方法一 +### 方法一:二维前缀和 + +根据题目描述,我们只需要统计每个位置 $(i, j)$ 的前缀和 $s[i][j][0]$ 和 $s[i][j][1]$,分别表示从 $(0, 0)$ 到 $(i, j)$ 的子矩阵中字符 `X` 和 `Y` 的数量,如果 $s[i][j][0] > 0$ 且 $s[i][j][0] = s[i][j][1]$,则说明满足题目条件,答案加一。 + +遍历完所有位置后,返回答案即可。 + +时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别表示矩阵的行数和列数。 #### Python3 ```python - +class Solution: + def numberOfSubmatrices(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + s = [[[0] * 2 for _ in range(n + 1)] for _ in range(m + 1)] + ans = 0 + for i, row in enumerate(grid, 1): + for j, x in enumerate(row, 1): + s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0] + s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1] + if x != ".": + s[i][j][ord(x) & 1] += 1 + if s[i][j][0] > 0 and s[i][j][0] == s[i][j][1]: + ans += 1 + return ans ``` #### Java ```java - +class Solution { + public int numberOfSubmatrices(char[][] grid) { + int m = grid.length, n = grid[0].length; + int[][][] s = new int[m + 1][n + 1][2]; + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0] + + (grid[i - 1][j - 1] == 'X' ? 1 : 0); + s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1] + + (grid[i - 1][j - 1] == 'Y' ? 1 : 0); + if (s[i][j][0] > 0 && s[i][j][0] == s[i][j][1]) { + ++ans; + } + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int numberOfSubmatrices(vector>& grid) { + int m = grid.size(), n = grid[0].size(); + vector>> s(m + 1, vector>(n + 1, vector(2))); + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0] + + (grid[i - 1][j - 1] == 'X' ? 1 : 0); + s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1] + + (grid[i - 1][j - 1] == 'Y' ? 1 : 0); + if (s[i][j][0] > 0 && s[i][j][0] == s[i][j][1]) { + ++ans; + } + } + } + return ans; + } +}; ``` #### Go ```go +func numberOfSubmatrices(grid [][]byte) (ans int) { + m, n := len(grid), len(grid[0]) + s := make([][][]int, m+1) + for i := range s { + s[i] = make([][]int, n+1) + for j := range s[i] { + s[i][j] = make([]int, 2) + } + } + + for i := 1; i <= m; i++ { + for j := 1; j <= n; j++ { + s[i][j][0] = s[i-1][j][0] + s[i][j-1][0] - s[i-1][j-1][0] + if grid[i-1][j-1] == 'X' { + s[i][j][0]++ + } + s[i][j][1] = s[i-1][j][1] + s[i][j-1][1] - s[i-1][j-1][1] + if grid[i-1][j-1] == 'Y' { + s[i][j][1]++ + } + if s[i][j][0] > 0 && s[i][j][0] == s[i][j][1] { + ans++ + } + } + } + return +} +``` +#### TypeScript + +```ts +function numberOfSubmatrices(grid: string[][]): number { + const [m, n] = [grid.length, grid[0].length]; + const s = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => [0, 0])); + let ans = 0; + + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j][0] = + s[i - 1][j][0] + + s[i][j - 1][0] - + s[i - 1][j - 1][0] + + (grid[i - 1][j - 1] === 'X' ? 1 : 0); + s[i][j][1] = + s[i - 1][j][1] + + s[i][j - 1][1] - + s[i - 1][j - 1][1] + + (grid[i - 1][j - 1] === 'Y' ? 1 : 0); + if (s[i][j][0] > 0 && s[i][j][0] === s[i][j][1]) { + ++ans; + } + } + } + + return ans; +} ``` diff --git a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README_EN.md b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README_EN.md index 65c510524f1c9..22f0246d90563 100644 --- a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README_EN.md +++ b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/README_EN.md @@ -73,32 +73,144 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3212.Co -### Solution 1 +### Solution 1: 2D Prefix Sum + +According to the problem description, we only need to calculate the prefix sums $s[i][j][0]$ and $s[i][j][1]$ for each position $(i, j)$, which represent the number of characters `X` and `Y` in the submatrix from $(0, 0)$ to $(i, j)$, respectively. If $s[i][j][0] > 0$ and $s[i][j][0] = s[i][j][1]$, it means the condition is met, and we increment the answer by one. + +After traversing all positions, return the answer. + +The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$. Here, $m$ and $n$ represent the number of rows and columns of the matrix, respectively. #### Python3 ```python - +class Solution: + def numberOfSubmatrices(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + s = [[[0] * 2 for _ in range(n + 1)] for _ in range(m + 1)] + ans = 0 + for i, row in enumerate(grid, 1): + for j, x in enumerate(row, 1): + s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0] + s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1] + if x != ".": + s[i][j][ord(x) & 1] += 1 + if s[i][j][0] > 0 and s[i][j][0] == s[i][j][1]: + ans += 1 + return ans ``` #### Java ```java - +class Solution { + public int numberOfSubmatrices(char[][] grid) { + int m = grid.length, n = grid[0].length; + int[][][] s = new int[m + 1][n + 1][2]; + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0] + + (grid[i - 1][j - 1] == 'X' ? 1 : 0); + s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1] + + (grid[i - 1][j - 1] == 'Y' ? 1 : 0); + if (s[i][j][0] > 0 && s[i][j][0] == s[i][j][1]) { + ++ans; + } + } + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + int numberOfSubmatrices(vector>& grid) { + int m = grid.size(), n = grid[0].size(); + vector>> s(m + 1, vector>(n + 1, vector(2))); + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0] + + (grid[i - 1][j - 1] == 'X' ? 1 : 0); + s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1] + + (grid[i - 1][j - 1] == 'Y' ? 1 : 0); + if (s[i][j][0] > 0 && s[i][j][0] == s[i][j][1]) { + ++ans; + } + } + } + return ans; + } +}; ``` #### Go ```go +func numberOfSubmatrices(grid [][]byte) (ans int) { + m, n := len(grid), len(grid[0]) + s := make([][][]int, m+1) + for i := range s { + s[i] = make([][]int, n+1) + for j := range s[i] { + s[i][j] = make([]int, 2) + } + } + + for i := 1; i <= m; i++ { + for j := 1; j <= n; j++ { + s[i][j][0] = s[i-1][j][0] + s[i][j-1][0] - s[i-1][j-1][0] + if grid[i-1][j-1] == 'X' { + s[i][j][0]++ + } + s[i][j][1] = s[i-1][j][1] + s[i][j-1][1] - s[i-1][j-1][1] + if grid[i-1][j-1] == 'Y' { + s[i][j][1]++ + } + if s[i][j][0] > 0 && s[i][j][0] == s[i][j][1] { + ans++ + } + } + } + return +} +``` +#### TypeScript + +```ts +function numberOfSubmatrices(grid: string[][]): number { + const [m, n] = [grid.length, grid[0].length]; + const s = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => [0, 0])); + let ans = 0; + + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j][0] = + s[i - 1][j][0] + + s[i][j - 1][0] - + s[i - 1][j - 1][0] + + (grid[i - 1][j - 1] === 'X' ? 1 : 0); + s[i][j][1] = + s[i - 1][j][1] + + s[i][j - 1][1] - + s[i - 1][j - 1][1] + + (grid[i - 1][j - 1] === 'Y' ? 1 : 0); + if (s[i][j][0] > 0 && s[i][j][0] === s[i][j][1]) { + ++ans; + } + } + } + + return ans; +} ``` diff --git a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.cpp b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.cpp new file mode 100644 index 0000000000000..dcf311ecf7c45 --- /dev/null +++ b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int numberOfSubmatrices(vector>& grid) { + int m = grid.size(), n = grid[0].size(); + vector>> s(m + 1, vector>(n + 1, vector(2))); + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0] + + (grid[i - 1][j - 1] == 'X' ? 1 : 0); + s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1] + + (grid[i - 1][j - 1] == 'Y' ? 1 : 0); + if (s[i][j][0] > 0 && s[i][j][0] == s[i][j][1]) { + ++ans; + } + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.go b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.go new file mode 100644 index 0000000000000..d7a57e57296b5 --- /dev/null +++ b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.go @@ -0,0 +1,27 @@ +func numberOfSubmatrices(grid [][]byte) (ans int) { + m, n := len(grid), len(grid[0]) + s := make([][][]int, m+1) + for i := range s { + s[i] = make([][]int, n+1) + for j := range s[i] { + s[i][j] = make([]int, 2) + } + } + + for i := 1; i <= m; i++ { + for j := 1; j <= n; j++ { + s[i][j][0] = s[i-1][j][0] + s[i][j-1][0] - s[i-1][j-1][0] + if grid[i-1][j-1] == 'X' { + s[i][j][0]++ + } + s[i][j][1] = s[i-1][j][1] + s[i][j-1][1] - s[i-1][j-1][1] + if grid[i-1][j-1] == 'Y' { + s[i][j][1]++ + } + if s[i][j][0] > 0 && s[i][j][0] == s[i][j][1] { + ans++ + } + } + } + return +} \ No newline at end of file diff --git a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.java b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.java new file mode 100644 index 0000000000000..12e65af5a83bf --- /dev/null +++ b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public int numberOfSubmatrices(char[][] grid) { + int m = grid.length, n = grid[0].length; + int[][][] s = new int[m + 1][n + 1][2]; + int ans = 0; + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0] + + (grid[i - 1][j - 1] == 'X' ? 1 : 0); + s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1] + + (grid[i - 1][j - 1] == 'Y' ? 1 : 0); + if (s[i][j][0] > 0 && s[i][j][0] == s[i][j][1]) { + ++ans; + } + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.py b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.py new file mode 100644 index 0000000000000..e6ba67420b011 --- /dev/null +++ b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.py @@ -0,0 +1,14 @@ +class Solution: + def numberOfSubmatrices(self, grid: List[List[str]]) -> int: + m, n = len(grid), len(grid[0]) + s = [[[0] * 2 for _ in range(n + 1)] for _ in range(m + 1)] + ans = 0 + for i, row in enumerate(grid, 1): + for j, x in enumerate(row, 1): + s[i][j][0] = s[i - 1][j][0] + s[i][j - 1][0] - s[i - 1][j - 1][0] + s[i][j][1] = s[i - 1][j][1] + s[i][j - 1][1] - s[i - 1][j - 1][1] + if x != ".": + s[i][j][ord(x) & 1] += 1 + if s[i][j][0] > 0 and s[i][j][0] == s[i][j][1]: + ans += 1 + return ans diff --git a/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.ts b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.ts new file mode 100644 index 0000000000000..d80423c6da2bc --- /dev/null +++ b/solution/3200-3299/3212.Count Submatrices With Equal Frequency of X and Y/Solution.ts @@ -0,0 +1,25 @@ +function numberOfSubmatrices(grid: string[][]): number { + const [m, n] = [grid.length, grid[0].length]; + const s = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => [0, 0])); + let ans = 0; + + for (let i = 1; i <= m; ++i) { + for (let j = 1; j <= n; ++j) { + s[i][j][0] = + s[i - 1][j][0] + + s[i][j - 1][0] - + s[i - 1][j - 1][0] + + (grid[i - 1][j - 1] === 'X' ? 1 : 0); + s[i][j][1] = + s[i - 1][j][1] + + s[i][j - 1][1] - + s[i - 1][j - 1][1] + + (grid[i - 1][j - 1] === 'Y' ? 1 : 0); + if (s[i][j][0] > 0 && s[i][j][0] === s[i][j][1]) { + ++ans; + } + } + } + + return ans; +}