diff --git a/solution/2900-2999/2937.Make Three Strings Equal/README.md b/solution/2900-2999/2937.Make Three Strings Equal/README.md index 64a0573aa76aa..d42c2146d7a5b 100644 --- a/solution/2900-2999/2937.Make Three Strings Equal/README.md +++ b/solution/2900-2999/2937.Make Three Strings Equal/README.md @@ -42,6 +42,12 @@ +**方法一:枚举** + +根据题目描述,我们知道,如果删除字符后的三个字符串相等,那么它们存在一个长度大于 1ドル$ 的公共前缀。因此,我们可以枚举公共前缀的位置 $i,ドル如果当前下标 $i$ 对应的三个字符不完全相等,那么公共前缀长度为 $i,ドル此时,我们判断 $i$ 是否为 0ドル,ドル若是,返回 $-1,ドル否则返回 $s - 3 \times i,ドル其中 $s$ 为三个字符串的长度和。 + +时间复杂度 $O(n),ドル其中 $n$ 为三个字符串的最小长度。空间复杂度 $O(1)$。 + ### **Python3** @@ -49,7 +55,14 @@ ```python - +class Solution: + def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int: + s = len(s1) + len(s2) + len(s3) + n = min(len(s1), len(s2), len(s3)) + for i in range(n): + if not s1[i] == s2[i] == s3[i]: + return -1 if i == 0 else s - 3 * i + return s - 3 * n ``` ### **Java** @@ -57,19 +70,69 @@ ```java - +class Solution { + public int findMinimumOperations(String s1, String s2, String s3) { + int s = s1.length() + s2.length() + s3.length(); + int n = Math.min(Math.min(s1.length(), s2.length()), s3.length()); + for (int i = 0; i < n; ++i) { + if (!(s1.charAt(i) == s2.charAt(i) && s2.charAt(i) == s3.charAt(i))) { + return i == 0 ? -1 : s - 3 * i; + } + } + return s - 3 * n; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int findMinimumOperations(string s1, string s2, string s3) { + int s = s1.size() + s2.size() + s3.size(); + int n = min({s1.size(), s2.size(), s3.size()}); + for (int i = 0; i < n; ++i) { + if (!(s1[i] == s2[i] && s2[i] == s3[i])) { + return i == 0 ? -1 : s - 3 * i; + } + } + return s - 3 * n; + } +}; ``` ### **Go** ```go +func findMinimumOperations(s1 string, s2 string, s3 string) int { + s := len(s1) + len(s2) + len(s3) + n := min(len(s1), len(s2), len(s3)) + for i := range s1[:n] { + if !(s1[i] == s2[i] && s2[i] == s3[i]) { + if i == 0 { + return -1 + } + return s - 3*i + } + } + return s - 3*n +} +``` +### **TypeScript** + +```ts +function findMinimumOperations(s1: string, s2: string, s3: string): number { + const s = s1.length + s2.length + s3.length; + const n = Math.min(s1.length, s2.length, s3.length); + for (let i = 0; i < n; ++i) { + if (!(s1[i] === s2[i] && s2[i] === s3[i])) { + return i === 0 ? -1 : s - 3 * i; + } + } + return s - 3 * n; +} ``` ### **...** diff --git a/solution/2900-2999/2937.Make Three Strings Equal/README_EN.md b/solution/2900-2999/2937.Make Three Strings Equal/README_EN.md index 60982a7864fd1..9bb57e7c374e9 100644 --- a/solution/2900-2999/2937.Make Three Strings Equal/README_EN.md +++ b/solution/2900-2999/2937.Make Three Strings Equal/README_EN.md @@ -37,30 +37,93 @@ It can be shown that there is no way to make them equal with less than two opera ## Solutions +**Solution 1: Enumeration** + +According to the problem description, we know that if the three strings are equal after deleting characters, then they have a common prefix of length greater than 1ドル$. Therefore, we can enumerate the position $i$ of the common prefix. If the three characters at the current index $i$ are not all equal, then the length of the common prefix is $i$. At this point, we check if $i$ is 0ドル$. If it is, return $-1$. Otherwise, return $s - 3 \times i,ドル where $s$ is the sum of the lengths of the three strings. + +The time complexity is $O(n),ドル where $n$ is the minimum length of the three strings. The space complexity is $O(1)$. + ### **Python3** ```python - +class Solution: + def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int: + s = len(s1) + len(s2) + len(s3) + n = min(len(s1), len(s2), len(s3)) + for i in range(n): + if not s1[i] == s2[i] == s3[i]: + return -1 if i == 0 else s - 3 * i + return s - 3 * n ``` ### **Java** ```java - +class Solution { + public int findMinimumOperations(String s1, String s2, String s3) { + int s = s1.length() + s2.length() + s3.length(); + int n = Math.min(Math.min(s1.length(), s2.length()), s3.length()); + for (int i = 0; i < n; ++i) { + if (!(s1.charAt(i) == s2.charAt(i) && s2.charAt(i) == s3.charAt(i))) { + return i == 0 ? -1 : s - 3 * i; + } + } + return s - 3 * n; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + int findMinimumOperations(string s1, string s2, string s3) { + int s = s1.size() + s2.size() + s3.size(); + int n = min({s1.size(), s2.size(), s3.size()}); + for (int i = 0; i < n; ++i) { + if (!(s1[i] == s2[i] && s2[i] == s3[i])) { + return i == 0 ? -1 : s - 3 * i; + } + } + return s - 3 * n; + } +}; ``` ### **Go** ```go +func findMinimumOperations(s1 string, s2 string, s3 string) int { + s := len(s1) + len(s2) + len(s3) + n := min(len(s1), len(s2), len(s3)) + for i := range s1[:n] { + if !(s1[i] == s2[i] && s2[i] == s3[i]) { + if i == 0 { + return -1 + } + return s - 3*i + } + } + return s - 3*n +} +``` +### **TypeScript** + +```ts +function findMinimumOperations(s1: string, s2: string, s3: string): number { + const s = s1.length + s2.length + s3.length; + const n = Math.min(s1.length, s2.length, s3.length); + for (let i = 0; i < n; ++i) { + if (!(s1[i] === s2[i] && s2[i] === s3[i])) { + return i === 0 ? -1 : s - 3 * i; + } + } + return s - 3 * n; +} ``` ### **...** diff --git a/solution/2900-2999/2937.Make Three Strings Equal/Solution.cpp b/solution/2900-2999/2937.Make Three Strings Equal/Solution.cpp new file mode 100644 index 0000000000000..07117f27784a8 --- /dev/null +++ b/solution/2900-2999/2937.Make Three Strings Equal/Solution.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int findMinimumOperations(string s1, string s2, string s3) { + int s = s1.size() + s2.size() + s3.size(); + int n = min({s1.size(), s2.size(), s3.size()}); + for (int i = 0; i < n; ++i) { + if (!(s1[i] == s2[i] && s2[i] == s3[i])) { + return i == 0 ? -1 : s - 3 * i; + } + } + return s - 3 * n; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2937.Make Three Strings Equal/Solution.go b/solution/2900-2999/2937.Make Three Strings Equal/Solution.go new file mode 100644 index 0000000000000..a5a8c393433a2 --- /dev/null +++ b/solution/2900-2999/2937.Make Three Strings Equal/Solution.go @@ -0,0 +1,13 @@ +func findMinimumOperations(s1 string, s2 string, s3 string) int { + s := len(s1) + len(s2) + len(s3) + n := min(len(s1), len(s2), len(s3)) + for i := range s1[:n] { + if !(s1[i] == s2[i] && s2[i] == s3[i]) { + if i == 0 { + return -1 + } + return s - 3*i + } + } + return s - 3*n +} \ No newline at end of file diff --git a/solution/2900-2999/2937.Make Three Strings Equal/Solution.java b/solution/2900-2999/2937.Make Three Strings Equal/Solution.java new file mode 100644 index 0000000000000..00fafdad8f947 --- /dev/null +++ b/solution/2900-2999/2937.Make Three Strings Equal/Solution.java @@ -0,0 +1,12 @@ +class Solution { + public int findMinimumOperations(String s1, String s2, String s3) { + int s = s1.length() + s2.length() + s3.length(); + int n = Math.min(Math.min(s1.length(), s2.length()), s3.length()); + for (int i = 0; i < n; ++i) { + if (!(s1.charAt(i) == s2.charAt(i) && s2.charAt(i) == s3.charAt(i))) { + return i == 0 ? -1 : s - 3 * i; + } + } + return s - 3 * n; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2937.Make Three Strings Equal/Solution.py b/solution/2900-2999/2937.Make Three Strings Equal/Solution.py new file mode 100644 index 0000000000000..026a39cccb941 --- /dev/null +++ b/solution/2900-2999/2937.Make Three Strings Equal/Solution.py @@ -0,0 +1,8 @@ +class Solution: + def findMinimumOperations(self, s1: str, s2: str, s3: str) -> int: + s = len(s1) + len(s2) + len(s3) + n = min(len(s1), len(s2), len(s3)) + for i in range(n): + if not s1[i] == s2[i] == s3[i]: + return -1 if i == 0 else s - 3 * i + return s - 3 * n diff --git a/solution/2900-2999/2937.Make Three Strings Equal/Solution.ts b/solution/2900-2999/2937.Make Three Strings Equal/Solution.ts new file mode 100644 index 0000000000000..60085e3bdd11a --- /dev/null +++ b/solution/2900-2999/2937.Make Three Strings Equal/Solution.ts @@ -0,0 +1,10 @@ +function findMinimumOperations(s1: string, s2: string, s3: string): number { + const s = s1.length + s2.length + s3.length; + const n = Math.min(s1.length, s2.length, s3.length); + for (let i = 0; i < n; ++i) { + if (!(s1[i] === s2[i] && s2[i] === s3[i])) { + return i === 0 ? -1 : s - 3 * i; + } + } + return s - 3 * n; +} diff --git a/solution/2900-2999/2938.Separate Black and White Balls/README.md b/solution/2900-2999/2938.Separate Black and White Balls/README.md index b202fb68d215d..b6f73c148981f 100644 --- a/solution/2900-2999/2938.Separate Black and White Balls/README.md +++ b/solution/2900-2999/2938.Separate Black and White Balls/README.md @@ -57,6 +57,14 @@ +**方法一:计数模拟** + +我们考虑将所有的 1ドル$ 移到最右边,用一个变量 $cnt$ 记录当前已经移动到最右边的 1ドル$ 的个数,用一个变量 $ans$ 记录移动的次数。 + +我们从右往左遍历字符串,如果当前位置是 1ドル,ドル那么我们将 $cnt$ 加一,同时将 $n - i - cnt$ 加到 $ans$ 中,其中 $n$ 是字符串的长度。最后返回 $ans$ 即可。 + +时间复杂度 $O(n),ドル其中 $n$ 是字符串的长度。空间复杂度 $O(1)$。 + ### **Python3** @@ -64,7 +72,15 @@ ```python - +class Solution: + def minimumSteps(self, s: str) -> int: + n = len(s) + ans = cnt = 0 + for i in range(n - 1, -1, -1): + if s[i] == '1': + cnt += 1 + ans += n - i - cnt + return ans ``` ### **Java** @@ -72,19 +88,72 @@ ```java - +class Solution { + public long minimumSteps(String s) { + long ans = 0; + int cnt = 0; + int n = s.length(); + for (int i = n - 1; i>= 0; --i) { + if (s.charAt(i) == '1') { + ++cnt; + ans += n - i - cnt; + } + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + long long minimumSteps(string s) { + long long ans = 0; + int cnt = 0; + int n = s.size(); + for (int i = n - 1; i>= 0; --i) { + if (s[i] == '1') { + ++cnt; + ans += n - i - cnt; + } + } + return ans; + } +}; ``` ### **Go** ```go +func minimumSteps(s string) (ans int64) { + n := len(s) + cnt := 0 + for i := n - 1; i>= 0; i-- { + if s[i] == '1' { + cnt++ + ans += int64(n - i - cnt) + } + } + return +} +``` +### **TypeScript** + +```ts +function minimumSteps(s: string): number { + const n = s.length; + let [ans, cnt] = [0, 0]; + for (let i = n - 1; ~i; --i) { + if (s[i] === '1') { + ++cnt; + ans += n - i - cnt; + } + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2938.Separate Black and White Balls/README_EN.md b/solution/2900-2999/2938.Separate Black and White Balls/README_EN.md index aba7028c96c57..b9ee2f1cc7cf9 100644 --- a/solution/2900-2999/2938.Separate Black and White Balls/README_EN.md +++ b/solution/2900-2999/2938.Separate Black and White Balls/README_EN.md @@ -51,30 +51,99 @@ It can be proven that the minimum number of steps needed is 2. ## Solutions +**Solution 1: Counting Simulation** + +We consider moving all the '1's to the rightmost side. We use a variable $cnt$ to record the current number of '1's that have been moved to the rightmost side, and a variable $ans$ to record the number of moves. + +We traverse the string from right to left. If the current position is '1', then we increment $cnt$ by one, and add $n - i - cnt$ to $ans,ドル where $n$ is the length of the string. Finally, we return $ans$. + +The time complexity is $O(n),ドル where $n$ is the length of the string. The space complexity is $O(1)$. + ### **Python3** ```python - +class Solution: + def minimumSteps(self, s: str) -> int: + n = len(s) + ans = cnt = 0 + for i in range(n - 1, -1, -1): + if s[i] == '1': + cnt += 1 + ans += n - i - cnt + return ans ``` ### **Java** ```java - +class Solution { + public long minimumSteps(String s) { + long ans = 0; + int cnt = 0; + int n = s.length(); + for (int i = n - 1; i>= 0; --i) { + if (s.charAt(i) == '1') { + ++cnt; + ans += n - i - cnt; + } + } + return ans; + } +} ``` ### **C++** ```cpp - +class Solution { +public: + long long minimumSteps(string s) { + long long ans = 0; + int cnt = 0; + int n = s.size(); + for (int i = n - 1; i>= 0; --i) { + if (s[i] == '1') { + ++cnt; + ans += n - i - cnt; + } + } + return ans; + } +}; ``` ### **Go** ```go +func minimumSteps(s string) (ans int64) { + n := len(s) + cnt := 0 + for i := n - 1; i>= 0; i-- { + if s[i] == '1' { + cnt++ + ans += int64(n - i - cnt) + } + } + return +} +``` +### **TypeScript** + +```ts +function minimumSteps(s: string): number { + const n = s.length; + let [ans, cnt] = [0, 0]; + for (let i = n - 1; ~i; --i) { + if (s[i] === '1') { + ++cnt; + ans += n - i - cnt; + } + } + return ans; +} ``` ### **...** diff --git a/solution/2900-2999/2938.Separate Black and White Balls/Solution.cpp b/solution/2900-2999/2938.Separate Black and White Balls/Solution.cpp new file mode 100644 index 0000000000000..e557a36231b11 --- /dev/null +++ b/solution/2900-2999/2938.Separate Black and White Balls/Solution.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + long long minimumSteps(string s) { + long long ans = 0; + int cnt = 0; + int n = s.size(); + for (int i = n - 1; i>= 0; --i) { + if (s[i] == '1') { + ++cnt; + ans += n - i - cnt; + } + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/2900-2999/2938.Separate Black and White Balls/Solution.go b/solution/2900-2999/2938.Separate Black and White Balls/Solution.go new file mode 100644 index 0000000000000..1ed5f9ac58617 --- /dev/null +++ b/solution/2900-2999/2938.Separate Black and White Balls/Solution.go @@ -0,0 +1,11 @@ +func minimumSteps(s string) (ans int64) { + n := len(s) + cnt := 0 + for i := n - 1; i>= 0; i-- { + if s[i] == '1' { + cnt++ + ans += int64(n - i - cnt) + } + } + return +} \ No newline at end of file diff --git a/solution/2900-2999/2938.Separate Black and White Balls/Solution.java b/solution/2900-2999/2938.Separate Black and White Balls/Solution.java new file mode 100644 index 0000000000000..b14a1f1588eb9 --- /dev/null +++ b/solution/2900-2999/2938.Separate Black and White Balls/Solution.java @@ -0,0 +1,14 @@ +class Solution { + public long minimumSteps(String s) { + long ans = 0; + int cnt = 0; + int n = s.length(); + for (int i = n - 1; i>= 0; --i) { + if (s.charAt(i) == '1') { + ++cnt; + ans += n - i - cnt; + } + } + return ans; + } +} \ No newline at end of file diff --git a/solution/2900-2999/2938.Separate Black and White Balls/Solution.py b/solution/2900-2999/2938.Separate Black and White Balls/Solution.py new file mode 100644 index 0000000000000..5487f26ce29cb --- /dev/null +++ b/solution/2900-2999/2938.Separate Black and White Balls/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def minimumSteps(self, s: str) -> int: + n = len(s) + ans = cnt = 0 + for i in range(n - 1, -1, -1): + if s[i] == '1': + cnt += 1 + ans += n - i - cnt + return ans diff --git a/solution/2900-2999/2938.Separate Black and White Balls/Solution.ts b/solution/2900-2999/2938.Separate Black and White Balls/Solution.ts new file mode 100644 index 0000000000000..3ee15e9ed7f79 --- /dev/null +++ b/solution/2900-2999/2938.Separate Black and White Balls/Solution.ts @@ -0,0 +1,11 @@ +function minimumSteps(s: string): number { + const n = s.length; + let [ans, cnt] = [0, 0]; + for (let i = n - 1; ~i; --i) { + if (s[i] === '1') { + ++cnt; + ans += n - i - cnt; + } + } + return ans; +}