From a59c45b4d4354c878e9999baad324c1c6a358d42 Mon Sep 17 00:00:00 2001 From: YXxy1002 <502400992@qq.com> Date: Tue, 27 Jul 2021 11:35:00 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1247.Minimum Swaps to Make Strings Equal --- .../README.md | 43 ++++++++++++++++++- .../README_EN.md | 41 ++++++++++++++++++ .../Solution.go | 18 ++++++++ .../Solution.java | 19 ++++++++ 4 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/Solution.go create mode 100644 solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/Solution.java diff --git a/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README.md b/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README.md index 116ed00c2999a..52c8c4b13eac3 100644 --- a/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README.md +++ b/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README.md @@ -70,10 +70,49 @@ ### **Java** - - ```java +class Solution { + public int minimumSwap(String s1, String s2) { + int xy = 0, yx = 0; + char[] c1 = s1.toCharArray(); + char[] c2 = s2.toCharArray(); + for (int i = 0; i < c1.length; i++) { + if (c1[i] > c2[i]) { + xy++; + } + if (c2[i] > c1[i]) { + yx++; + } + } + if ((xy + yx) % 2 != 0) { + return -1; + } + return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1; + } +} +``` +### **Go** + +```go +func minimumSwap(s1 string, s2 string) int { + xy, yx := 0, 0 + for i, _ := range s1 { + if s1[i] < s2[i] { + xy++ + } + if s1[i] > s2[i] { + yx++ + } + } + if (xy+yx)%2 != 0 { + return -1 + } + if xy%2 == 0 { + return (xy + yx) / 2 + } + return (xy+yx)/2 + 1 +} ``` ### **...** diff --git a/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README_EN.md b/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README_EN.md index 288afa7930e07..7787678f1a2b3 100644 --- a/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README_EN.md +++ b/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/README_EN.md @@ -102,7 +102,48 @@ Note that you can't swap s1[0] and s1[1] to make s1 equal to "yx", ### **Java** ```java +class Solution { + public int minimumSwap(String s1, String s2) { + int xy = 0, yx = 0; + char[] c1 = s1.toCharArray(); + char[] c2 = s2.toCharArray(); + for (int i = 0; i < c1.length; i++) { + if (c1[i] > c2[i]) { + xy++; + } + if (c2[i] > c1[i]) { + yx++; + } + } + if ((xy + yx) % 2 != 0) { + return -1; + } + return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1; + } +} +``` +### **Go** + +```go +func minimumSwap(s1 string, s2 string) int { + xy, yx := 0, 0 + for i, _ := range s1 { + if s1[i] < s2[i] { + xy++ + } + if s1[i] > s2[i] { + yx++ + } + } + if (xy+yx)%2 != 0 { + return -1 + } + if xy%2 == 0 { + return (xy + yx) / 2 + } + return (xy+yx)/2 + 1 +} ``` ### **...** diff --git a/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/Solution.go b/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/Solution.go new file mode 100644 index 0000000000000..5fc8850fe0ae9 --- /dev/null +++ b/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/Solution.go @@ -0,0 +1,18 @@ +func minimumSwap(s1 string, s2 string) int { + xy, yx := 0, 0 + for i, _ := range s1 { + if s1[i] < s2[i] { + xy++ + } + if s1[i] > s2[i] { + yx++ + } + } + if (xy+yx)%2 != 0 { + return -1 + } + if xy%2 == 0 { + return (xy + yx) / 2 + } + return (xy+yx)/2 + 1 +} \ No newline at end of file diff --git a/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/Solution.java b/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/Solution.java new file mode 100644 index 0000000000000..eff5c356deec9 --- /dev/null +++ b/solution/1200-1299/1247.Minimum Swaps to Make Strings Equal/Solution.java @@ -0,0 +1,19 @@ +class Solution { + public int minimumSwap(String s1, String s2) { + int xy = 0, yx = 0; + char[] c1 = s1.toCharArray(); + char[] c2 = s2.toCharArray(); + for (int i = 0; i < c1.length; i++) { + if (c1[i] > c2[i]) { + xy++; + } + if (c2[i] > c1[i]) { + yx++; + } + } + if ((xy + yx) % 2 != 0) { + return -1; + } + return xy % 2 == 0 ? (xy + yx) / 2 : (xy + yx) / 2 + 1; + } +} \ No newline at end of file