diff --git a/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/README.md b/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/README.md index 7d148099a..131d0aa4c 100755 --- a/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/README.md +++ b/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/README.md @@ -1,28 +1,33 @@ # [1790.Check if One String Swap Can Make Strings Equal][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given two strings `s1` and `s2` of equal length. A **string swap** is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices. + +Return true if it is possible to make both strings equal by performing **at most one string swap** on **exactly one** of the strings. Otherwise, return `false`. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s1 = "bank", s2 = "kanb" +Output: true +Explanation: For example, swap the first character with the last character of s2 to make "bank". ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Check if One String Swap Can Make Strings Equal -```go +``` +Input: s1 = "attack", s2 = "defend" +Output: false +Explanation: It is impossible to make them equal with one string swap. ``` +**Example 3:** + +``` +Input: s1 = "kelb", s2 = "kelb" +Output: true +Explanation: The two strings are already equal, so no string swap operation is required. +``` ## 结语 diff --git a/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/Solution.go b/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/Solution.go index d115ccf5e..cf78a087f 100644 --- a/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/Solution.go +++ b/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/Solution.go @@ -1,5 +1,27 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s1 string, s2 string) bool { + if s1 == s2 { + return true + } + + var a, b uint8 + diff := 0 + for i := range s1 { + if s1[i] == s2[i] { + continue + } + diff++ + if diff == 1 { + a, b = s1[i], s2[i] + } else if diff == 2 { + // a, s1, b s2 + if !(a == s2[i] && b == s1[i]) { + return false + } + } else { + return false + } + } + return diff != 1 } diff --git a/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/Solution_test.go b/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/Solution_test.go index 14ff50eb4..6bc9f4baf 100644 --- a/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/Solution_test.go +++ b/leetcode/1701-1800/1790.Check-if-One-String-Swap-Can-Make-Strings-Equal/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + s1, s2 string expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "bank", "kanb", true}, + {"TestCase2", "attack", "defend", false}, + {"TestCase3", "kelb", "kelb", true}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.s1, c.s2) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.s1, c.s2) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }