Skip to content

Commit 9abb3dd

Browse files
committed
+ problem 1657
1 parent 4cc5f3e commit 9abb3dd

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 1657. Determine if Two Strings Are Close
2+
Two strings are considered **close** if you can attain one from the other using the following operations:
3+
4+
* Operation 1: Swap any two **existing** characters.
5+
* For example, `abcde -> aecdb`
6+
* Operation 2: Transform **every** occurrence of one **existing** character into another **existing** character, and do the same with the other character.
7+
* For example, `aacabb -> bbcbaa` (all `a`'s turn into `b`'s, and all `b`'s turn into `a`'s)
8+
9+
You can use the operations on either string as many times as necessary.
10+
11+
Given two strings, `word1` and `word2`, return `true` *if* `word1` *and* `word2` *are **close**, and* `false` *otherwise*.
12+
13+
#### Example 1:
14+
<pre>
15+
<strong>Input:</strong> word1 = "abc", word2 = "bca"
16+
<strong>Output:</strong> true
17+
<strong>Explanation:</strong> You can attain word2 from word1 in 2 operations.
18+
Apply Operation 1: "abc" -> "acb"
19+
Apply Operation 1: "acb" -> "bca"
20+
</pre>
21+
22+
#### Example 2:
23+
<pre>
24+
<strong>Input:</strong> word1 = "a", word2 = "aa"
25+
<strong>Output:</strong> false
26+
<strong>Explanation:</strong> It is impossible to attain word2 from word1, or vice versa, in any number of operations.
27+
</pre>
28+
29+
#### Example 3:
30+
<pre>
31+
<strong>Input:</strong> word1 = "cabbba", word2 = "abbccc"
32+
<strong>Output:</strong> true
33+
<strong>Explanation:</strong> You can attain word2 from word1 in 3 operations.
34+
Apply Operation 1: "cabbba" -> "caabbb"
35+
Apply Operation 2: "caabbb" -> "baaccc"
36+
Apply Operation 2: "baaccc" -> "abbccc"
37+
</pre>
38+
39+
#### Constraints:
40+
* <code>1 <= word1.length, word2.length <= 10<sup>5</sup></code>
41+
* `word1` and `word2` contain only lowercase English letters.
42+
43+
## Solutions (Python)
44+
45+
### 1. Solution
46+
```Python
47+
class Solution:
48+
def closeStrings(self, word1: str, word2: str) -> bool:
49+
if len(word1) != len(word2) or set(word1) != set(word2):
50+
return False
51+
52+
count1 = [0] * 26
53+
count2 = [0] * 26
54+
55+
for ch1, ch2 in zip(word1, word2):
56+
count1[ord(ch1) - 97] += 1
57+
count2[ord(ch2) - 97] += 1
58+
59+
return sorted(count1) == sorted(count2)
60+
```
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# 1657. 确定两个字符串是否接近
2+
如果可以使用以下操作从一个字符串得到另一个字符串,则认为两个字符串 **接近**
3+
4+
* 操作 1:交换任意两个 **现有** 字符。
5+
* 例如,`abcde -> aecdb`
6+
* 操作 2:将一个 **现有** 字符的每次出现转换为另一个 **现有** 字符,并对另一个字符执行相同的操作。
7+
* 例如,`aacabb -> bbcbaa`(所有 `a` 转化为 `b` ,而所有的 `b` 转换为 `a`
8+
9+
你可以根据需要对任意一个字符串多次使用这两种操作。
10+
11+
给你两个字符串,`word1``word2` 。如果 `word1``word2` **接近** ,就返回 `true` ;否则,返回 `false`
12+
13+
#### 示例 1:
14+
<pre>
15+
<strong>输入:</strong> word1 = "abc", word2 = "bca"
16+
<strong>输出:</strong> true
17+
<strong>解释:</strong> 2 次操作从 word1 获得 word2 。
18+
执行操作 1:"abc" -> "acb"
19+
执行操作 1:"acb" -> "bca"
20+
</pre>
21+
22+
#### 示例 2:
23+
<pre>
24+
<strong>输入:</strong> word1 = "a", word2 = "aa"
25+
<strong>输出:</strong> false
26+
<strong>解释:</strong> 不管执行多少次操作,都无法从 word1 得到 word2 ,反之亦然。
27+
</pre>
28+
29+
#### 示例 3:
30+
<pre>
31+
<strong>输入:</strong> word1 = "cabbba", word2 = "abbccc"
32+
<strong>输出:</strong> true
33+
<strong>解释:</strong> 3 次操作从 word1 获得 word2 。
34+
执行操作 1:"cabbba" -> "caabbb"
35+
执行操作 2:"caabbb" -> "baaccc"
36+
执行操作 2:"baaccc" -> "abbccc"
37+
</pre>
38+
39+
#### 示例 4:
40+
<pre>
41+
<strong>输入:</strong> word1 = "cabbba", word2 = "aabbss"
42+
<strong>输出:</strong> false
43+
<strong>解释:</strong> 不管执行多少次操作,都无法从 word1 得到 word2 ,反之亦然。
44+
</pre>
45+
46+
#### 提示:
47+
* <code>1 <= word1.length, word2.length <= 10<sup>5</sup></code>
48+
* `word1``word2` 仅包含小写英文字母
49+
50+
## 题解 (Python)
51+
52+
### 1. 题解
53+
```Python
54+
class Solution:
55+
def closeStrings(self, word1: str, word2: str) -> bool:
56+
if len(word1) != len(word2) or set(word1) != set(word2):
57+
return False
58+
59+
count1 = [0] * 26
60+
count2 = [0] * 26
61+
62+
for ch1, ch2 in zip(word1, word2):
63+
count1[ord(ch1) - 97] += 1
64+
count2[ord(ch2) - 97] += 1
65+
66+
return sorted(count1) == sorted(count2)
67+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def closeStrings(self, word1: str, word2: str) -> bool:
3+
if len(word1) != len(word2) or set(word1) != set(word2):
4+
return False
5+
6+
count1 = [0] * 26
7+
count2 = [0] * 26
8+
9+
for ch1, ch2 in zip(word1, word2):
10+
count1[ord(ch1) - 97] += 1
11+
count2[ord(ch2) - 97] += 1
12+
13+
return sorted(count1) == sorted(count2)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@
896896
[1652][1652l]|[Defuse the Bomb][1652] |![rb]&nbsp;&nbsp;![rs]
897897
[1653][1653l]|[Minimum Deletions to Make String Balanced][1653] |![rs]
898898
[1656][1656l]|[Design an Ordered Stream][1656] |![py]
899+
[1657][1657l]|[Determine if Two Strings Are Close][1657] |![py]
899900
[1658][1658l]|[Minimum Operations to Reduce X to Zero][1658] |![rb]&nbsp;&nbsp;![rs]
900901
[1662][1662l]|[Check If Two String Arrays are Equivalent][1662] |![rb]&nbsp;&nbsp;![rs]
901902
[1663][1663l]|[Smallest String With A Given Numeric Value][1663] |![rb]&nbsp;&nbsp;![rs]
@@ -2221,6 +2222,7 @@
22212222
[1652]:Problemset/1652-Defuse%20the%20Bomb/README.md#1652-defuse-the-bomb
22222223
[1653]:Problemset/1653-Minimum%20Deletions%20to%20Make%20String%20Balanced/README.md#1653-minimum-deletions-to-make-string-balanced
22232224
[1656]:Problemset/1656-Design%20an%20Ordered%20Stream/README.md#1656-design-an-ordered-stream
2225+
[1657]:Problemset/1657-Determine%20if%20Two%20Strings%20Are%20Close/README.md#1657-determine-if-two-strings-are-close
22242226
[1658]:Problemset/1658-Minimum%20Operations%20to%20Reduce%20X%20to%20Zero/README.md#1658-minimum-operations-to-reduce-x-to-zero
22252227
[1662]:Problemset/1662-Check%20If%20Two%20String%20Arrays%20are%20Equivalent/README.md#1662-check-if-two-string-arrays-are-equivalent
22262228
[1663]:Problemset/1663-Smallest%20String%20With%20A%20Given%20Numeric%20Value/README.md#1663-smallest-string-with-a-given-numeric-value
@@ -3549,6 +3551,7 @@
35493551
[1652l]:https://leetcode.com/problems/defuse-the-bomb/
35503552
[1653l]:https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/
35513553
[1656l]:https://leetcode.com/problems/design-an-ordered-stream/
3554+
[1657l]:https://leetcode.com/problems/determine-if-two-strings-are-close/
35523555
[1658l]:https://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/
35533556
[1662l]:https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/
35543557
[1663l]:https://leetcode.com/problems/smallest-string-with-a-given-numeric-value/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@
896896
[1652][1652l]|[拆炸弹][1652] |![rb]&nbsp;&nbsp;![rs]
897897
[1653][1653l]|[使字符串平衡的最少删除次数][1653] |![rs]
898898
[1656][1656l]|[设计有序流][1656] |![py]
899+
[1657][1657l]|[确定两个字符串是否接近][1657] |![py]
899900
[1658][1658l]|[将 x 减到 0 的最小操作数][1658] |![rb]&nbsp;&nbsp;![rs]
900901
[1662][1662l]|[检查两个字符串数组是否相等][1662] |![rb]&nbsp;&nbsp;![rs]
901902
[1663][1663l]|[具有给定数值的最小字符串][1663] |![rb]&nbsp;&nbsp;![rs]
@@ -2221,6 +2222,7 @@
22212222
[1652]:Problemset/1652-Defuse%20the%20Bomb/README_CN.md#1652-拆炸弹
22222223
[1653]:Problemset/1653-Minimum%20Deletions%20to%20Make%20String%20Balanced/README_CN.md#1653-使字符串平衡的最少删除次数
22232224
[1656]:Problemset/1656-Design%20an%20Ordered%20Stream/README_CN.md#1656-设计有序流
2225+
[1657]:Problemset/1657-Determine%20if%20Two%20Strings%20Are%20Close/README_CN.md#1657-确定两个字符串是否接近
22242226
[1658]:Problemset/1658-Minimum%20Operations%20to%20Reduce%20X%20to%20Zero/README_CN.md#1658-将-x-减到-0-的最小操作数
22252227
[1662]:Problemset/1662-Check%20If%20Two%20String%20Arrays%20are%20Equivalent/README_CN.md#1662-检查两个字符串数组是否相等
22262228
[1663]:Problemset/1663-Smallest%20String%20With%20A%20Given%20Numeric%20Value/README_CN.md#1663-具有给定数值的最小字符串
@@ -3549,6 +3551,7 @@
35493551
[1652l]:https://leetcode.cn/problems/defuse-the-bomb/
35503552
[1653l]:https://leetcode.cn/problems/minimum-deletions-to-make-string-balanced/
35513553
[1656l]:https://leetcode.cn/problems/design-an-ordered-stream/
3554+
[1657l]:https://leetcode.cn/problems/determine-if-two-strings-are-close/
35523555
[1658l]:https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/
35533556
[1662l]:https://leetcode.cn/problems/check-if-two-string-arrays-are-equivalent/
35543557
[1663l]:https://leetcode.cn/problems/smallest-string-with-a-given-numeric-value/

0 commit comments

Comments
 (0)