Skip to content

Commit 00fbf38

Browse files
committed
1790
1 parent 8bb7993 commit 00fbf38

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

.github/workflows/badge.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
steps:
1717
- uses: actions/checkout@v3
1818

19+
1920
- uses: shuzijun/leetcode-editor-action@v0.1.1
2021
with:
2122
STATISTICS_DIRECTORY: .idea/leetcode-pro/

.idea/leetcode-pro/editor.xml

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import "testing"
4+
5+
/**
6+
<p>给你长度相等的两个字符串 <code>s1</code> 和 <code>s2</code> 。一次<strong> 字符串交换 </strong>操作的步骤如下:选出某个字符串中的两个下标(不必不同),并交换这两个下标所对应的字符。</p>
7+
8+
<p>如果对 <strong>其中一个字符串</strong> 执行 <strong>最多一次字符串交换</strong> 就可以使两个字符串相等,返回 <code>true</code> ;否则,返回 <code>false</code> 。</p>
9+
10+
<p>&nbsp;</p>
11+
12+
<p><strong>示例 1:</strong></p>
13+
14+
<pre><strong>输入:</strong>s1 = "bank", s2 = "kanb"
15+
<strong>输出:</strong>true
16+
<strong>解释:</strong>例如,交换 s2 中的第一个和最后一个字符可以得到 "bank"
17+
</pre>
18+
19+
<p><strong>示例 2:</strong></p>
20+
21+
<pre><strong>输入:</strong>s1 = "attack", s2 = "defend"
22+
<strong>输出:</strong>false
23+
<strong>解释:</strong>一次字符串交换无法使两个字符串相等
24+
</pre>
25+
26+
<p><strong>示例 3:</strong></p>
27+
28+
<pre><strong>输入:</strong>s1 = "kelb", s2 = "kelb"
29+
<strong>输出:</strong>true
30+
<strong>解释:</strong>两个字符串已经相等,所以不需要进行字符串交换
31+
</pre>
32+
33+
<p><strong>示例 4:</strong></p>
34+
35+
<pre><strong>输入:</strong>s1 = "abcd", s2 = "dcba"
36+
<strong>输出:</strong>false
37+
</pre>
38+
39+
<p>&nbsp;</p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>1 &lt;= s1.length, s2.length &lt;= 100</code></li>
45+
<li><code>s1.length == s2.length</code></li>
46+
<li><code>s1</code> 和 <code>s2</code> 仅由小写英文字母组成</li>
47+
</ul>
48+
49+
<div><details><summary>Related Topics</summary><div><li>哈希表</li><li>字符串</li><li>计数</li></div></details></div>
50+
<div><li>👍 78</li><li>👎 0</li></div>
51+
*/
52+
53+
func TestAreAlmostEqual(t *testing.T) {
54+
t.Log(areAlmostEqual("bank", "kanb"))
55+
}
56+
57+
//leetcode submit region begin(Prohibit modification and deletion)
58+
func areAlmostEqual(s1 string, s2 string) bool {
59+
i, j := -1, -1
60+
for k := 0; k < len(s1); k++ {
61+
if s1[k] != s2[k] {
62+
if i == -1 {
63+
i = k
64+
} else if j == -1 {
65+
j = k
66+
} else {
67+
return false
68+
}
69+
}
70+
}
71+
72+
return i == -1 || j != -1 && s1[i] == s2[j] && s1[j] == s2[i]
73+
}
74+
75+
//leetcode submit region end(Prohibit modification and deletion)

0 commit comments

Comments
 (0)