From def7b8304ca6ec53bee4835391328d04641aa58b Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sun, 24 Nov 2024 10:19:49 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3361 No.3361.Shift Distance Between Two Strings --- .../README.md | 111 +++++++++++++++++- .../README_EN.md | 111 +++++++++++++++++- .../Solution.cpp | 23 ++++ .../Solution.go | 25 ++++ .../Solution.java | 20 ++++ .../Solution.py | 17 +++ .../Solution.ts | 19 +++ 7 files changed, 320 insertions(+), 6 deletions(-) create mode 100644 solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.cpp create mode 100644 solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.go create mode 100644 solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.java create mode 100644 solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.py create mode 100644 solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.ts diff --git a/solution/3300-3399/3361.Shift Distance Between Two Strings/README.md b/solution/3300-3399/3361.Shift Distance Between Two Strings/README.md index 20ee82b17d7f4..b817c838cfded 100644 --- a/solution/3300-3399/3361.Shift Distance Between Two Strings/README.md +++ b/solution/3300-3399/3361.Shift Distance Between Two Strings/README.md @@ -87,25 +87,130 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3361.Sh #### Python3 ```python - +class Solution: + def shiftDistance( + self, s: str, t: str, nextCost: List[int], previousCost: List[int] + ) -> int: + m = 26 + s1 = [0] * (m << 1 | 1) + s2 = [0] * (m << 1 | 1) + for i in range(m << 1): + s1[i + 1] = s1[i] + nextCost[i % m] + s2[i + 1] = s2[i] + previousCost[(i + 1) % m] + ans = 0 + for a, b in zip(s, t): + x, y = ord(a) - ord("a"), ord(b) - ord("a") + c1 = s1[y + m if y < x else y] - s1[x] + c2 = s2[x + m if x < y else x] - s2[y] + ans += min(c1, c2) + return ans ``` #### Java ```java - +class Solution { + public long shiftDistance(String s, String t, int[] nextCost, int[] previousCost) { + int m = 26; + long[] s1 = new long[(m << 1) + 1]; + long[] s2 = new long[(m << 1) + 1]; + for (int i = 0; i < (m << 1); i++) { + s1[i + 1] = s1[i] + nextCost[i % m]; + s2[i + 1] = s2[i] + previousCost[(i + 1) % m]; + } + long ans = 0; + for (int i = 0; i < s.length(); i++) { + int x = s.charAt(i) - 'a'; + int y = t.charAt(i) - 'a'; + long c1 = s1[y + (y < x ? m : 0)] - s1[x]; + long c2 = s2[x + (x < y ? m : 0)] - s2[y]; + ans += Math.min(c1, c2); + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long shiftDistance(string s, string t, vector& nextCost, vector& previousCost) { + int m = 26; + vector s1((m << 1) + 1); + vector s2((m << 1) + 1); + for (int i = 0; i < (m << 1); ++i) { + s1[i + 1] = s1[i] + nextCost[i % m]; + s2[i + 1] = s2[i] + previousCost[(i + 1) % m]; + } + + long long ans = 0; + for (int i = 0; i < s.size(); ++i) { + int x = s[i] - 'a'; + int y = t[i] - 'a'; + long long c1 = s1[y + (y < x ? m : 0)] - s1[x]; + long long c2 = s2[x + (x < y ? m : 0)] - s2[y]; + ans += min(c1, c2); + } + + return ans; + } +}; ``` #### Go ```go +func shiftDistance(s string, t string, nextCost []int, previousCost []int) (ans int64) { + m := 26 + s1 := make([]int64, (m<<1)+1) + s2 := make([]int64, (m<<1)+1) + for i := 0; i < (m << 1); i++ { + s1[i+1] = s1[i] + int64(nextCost[i%m]) + s2[i+1] = s2[i] + int64(previousCost[(i+1)%m]) + } + for i := 0; i < len(s); i++ { + x := int(s[i] - 'a') + y := int(t[i] - 'a') + z := y + if y < x { + z += m + } + c1 := s1[z] - s1[x] + z = x + if x < y { + z += m + } + c2 := s2[z] - s2[y] + ans += min(c1, c2) + } + return +} +``` +#### TypeScript + +```ts +function shiftDistance(s: string, t: string, nextCost: number[], previousCost: number[]): number { + const m = 26; + const s1: number[] = Array((m << 1) + 1).fill(0); + const s2: number[] = Array((m << 1) + 1).fill(0); + for (let i = 0; i < m << 1; i++) { + s1[i + 1] = s1[i] + nextCost[i % m]; + s2[i + 1] = s2[i] + previousCost[(i + 1) % m]; + } + let ans = 0; + const a = 'a'.charCodeAt(0); + for (let i = 0; i < s.length; i++) { + const x = s.charCodeAt(i) - a; + const y = t.charCodeAt(i) - a; + const c1 = s1[y + (y < x ? m : 0)] - s1[x]; + const c2 = s2[x + (x < y ? m : 0)] - s2[y]; + ans += Math.min(c1, c2); + } + return ans; +} ``` diff --git a/solution/3300-3399/3361.Shift Distance Between Two Strings/README_EN.md b/solution/3300-3399/3361.Shift Distance Between Two Strings/README_EN.md index 6ac1cf8be39cc..f5fc4a673f3d1 100644 --- a/solution/3300-3399/3361.Shift Distance Between Two Strings/README_EN.md +++ b/solution/3300-3399/3361.Shift Distance Between Two Strings/README_EN.md @@ -85,25 +85,130 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3361.Sh #### Python3 ```python - +class Solution: + def shiftDistance( + self, s: str, t: str, nextCost: List[int], previousCost: List[int] + ) -> int: + m = 26 + s1 = [0] * (m << 1 | 1) + s2 = [0] * (m << 1 | 1) + for i in range(m << 1): + s1[i + 1] = s1[i] + nextCost[i % m] + s2[i + 1] = s2[i] + previousCost[(i + 1) % m] + ans = 0 + for a, b in zip(s, t): + x, y = ord(a) - ord("a"), ord(b) - ord("a") + c1 = s1[y + m if y < x else y] - s1[x] + c2 = s2[x + m if x < y else x] - s2[y] + ans += min(c1, c2) + return ans ``` #### Java ```java - +class Solution { + public long shiftDistance(String s, String t, int[] nextCost, int[] previousCost) { + int m = 26; + long[] s1 = new long[(m << 1) + 1]; + long[] s2 = new long[(m << 1) + 1]; + for (int i = 0; i < (m << 1); i++) { + s1[i + 1] = s1[i] + nextCost[i % m]; + s2[i + 1] = s2[i] + previousCost[(i + 1) % m]; + } + long ans = 0; + for (int i = 0; i < s.length(); i++) { + int x = s.charAt(i) - 'a'; + int y = t.charAt(i) - 'a'; + long c1 = s1[y + (y < x ? m : 0)] - s1[x]; + long c2 = s2[x + (x < y ? m : 0)] - s2[y]; + ans += Math.min(c1, c2); + } + return ans; + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long shiftDistance(string s, string t, vector& nextCost, vector& previousCost) { + int m = 26; + vector s1((m << 1) + 1); + vector s2((m << 1) + 1); + for (int i = 0; i < (m << 1); ++i) { + s1[i + 1] = s1[i] + nextCost[i % m]; + s2[i + 1] = s2[i] + previousCost[(i + 1) % m]; + } + + long long ans = 0; + for (int i = 0; i < s.size(); ++i) { + int x = s[i] - 'a'; + int y = t[i] - 'a'; + long long c1 = s1[y + (y < x ? m : 0)] - s1[x]; + long long c2 = s2[x + (x < y ? m : 0)] - s2[y]; + ans += min(c1, c2); + } + + return ans; + } +}; ``` #### Go ```go +func shiftDistance(s string, t string, nextCost []int, previousCost []int) (ans int64) { + m := 26 + s1 := make([]int64, (m<<1)+1) + s2 := make([]int64, (m<<1)+1) + for i := 0; i < (m << 1); i++ { + s1[i+1] = s1[i] + int64(nextCost[i%m]) + s2[i+1] = s2[i] + int64(previousCost[(i+1)%m]) + } + for i := 0; i < len(s); i++ { + x := int(s[i] - 'a') + y := int(t[i] - 'a') + z := y + if y < x { + z += m + } + c1 := s1[z] - s1[x] + z = x + if x < y { + z += m + } + c2 := s2[z] - s2[y] + ans += min(c1, c2) + } + return +} +``` +#### TypeScript + +```ts +function shiftDistance(s: string, t: string, nextCost: number[], previousCost: number[]): number { + const m = 26; + const s1: number[] = Array((m << 1) + 1).fill(0); + const s2: number[] = Array((m << 1) + 1).fill(0); + for (let i = 0; i < m << 1; i++) { + s1[i + 1] = s1[i] + nextCost[i % m]; + s2[i + 1] = s2[i] + previousCost[(i + 1) % m]; + } + let ans = 0; + const a = 'a'.charCodeAt(0); + for (let i = 0; i < s.length; i++) { + const x = s.charCodeAt(i) - a; + const y = t.charCodeAt(i) - a; + const c1 = s1[y + (y < x ? m : 0)] - s1[x]; + const c2 = s2[x + (x < y ? m : 0)] - s2[y]; + ans += Math.min(c1, c2); + } + return ans; +} ``` diff --git a/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.cpp b/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.cpp new file mode 100644 index 0000000000000..c4ff1a2bcc4ea --- /dev/null +++ b/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + long long shiftDistance(string s, string t, vector& nextCost, vector& previousCost) { + int m = 26; + vector s1((m << 1) + 1); + vector s2((m << 1) + 1); + for (int i = 0; i < (m << 1); ++i) { + s1[i + 1] = s1[i] + nextCost[i % m]; + s2[i + 1] = s2[i] + previousCost[(i + 1) % m]; + } + + long long ans = 0; + for (int i = 0; i < s.size(); ++i) { + int x = s[i] - 'a'; + int y = t[i] - 'a'; + long long c1 = s1[y + (y < x ? m : 0)] - s1[x]; + long long c2 = s2[x + (x < y ? m : 0)] - s2[y]; + ans += min(c1, c2); + } + + return ans; + } +}; diff --git a/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.go b/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.go new file mode 100644 index 0000000000000..ff38c64f2f2e1 --- /dev/null +++ b/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.go @@ -0,0 +1,25 @@ +func shiftDistance(s string, t string, nextCost []int, previousCost []int) (ans int64) { + m := 26 + s1 := make([]int64, (m<<1)+1) + s2 := make([]int64, (m<<1)+1) + for i := 0; i < (m << 1); i++ { + s1[i+1] = s1[i] + int64(nextCost[i%m]) + s2[i+1] = s2[i] + int64(previousCost[(i+1)%m]) + } + for i := 0; i < len(s); i++ { + x := int(s[i] - 'a') + y := int(t[i] - 'a') + z := y + if y < x { + z += m + } + c1 := s1[z] - s1[x] + z = x + if x < y { + z += m + } + c2 := s2[z] - s2[y] + ans += min(c1, c2) + } + return +} diff --git a/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.java b/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.java new file mode 100644 index 0000000000000..51e8a283f104e --- /dev/null +++ b/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.java @@ -0,0 +1,20 @@ +class Solution { + public long shiftDistance(String s, String t, int[] nextCost, int[] previousCost) { + int m = 26; + long[] s1 = new long[(m << 1) + 1]; + long[] s2 = new long[(m << 1) + 1]; + for (int i = 0; i < (m << 1); i++) { + s1[i + 1] = s1[i] + nextCost[i % m]; + s2[i + 1] = s2[i] + previousCost[(i + 1) % m]; + } + long ans = 0; + for (int i = 0; i < s.length(); i++) { + int x = s.charAt(i) - 'a'; + int y = t.charAt(i) - 'a'; + long c1 = s1[y + (y < x ? m : 0)] - s1[x]; + long c2 = s2[x + (x < y ? m : 0)] - s2[y]; + ans += Math.min(c1, c2); + } + return ans; + } +} diff --git a/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.py b/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.py new file mode 100644 index 0000000000000..f78da8f5c6d69 --- /dev/null +++ b/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.py @@ -0,0 +1,17 @@ +class Solution: + def shiftDistance( + self, s: str, t: str, nextCost: List[int], previousCost: List[int] + ) -> int: + m = 26 + s1 = [0] * (m << 1 | 1) + s2 = [0] * (m << 1 | 1) + for i in range(m << 1): + s1[i + 1] = s1[i] + nextCost[i % m] + s2[i + 1] = s2[i] + previousCost[(i + 1) % m] + ans = 0 + for a, b in zip(s, t): + x, y = ord(a) - ord("a"), ord(b) - ord("a") + c1 = s1[y + m if y < x else y] - s1[x] + c2 = s2[x + m if x < y else x] - s2[y] + ans += min(c1, c2) + return ans diff --git a/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.ts b/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.ts new file mode 100644 index 0000000000000..016184778ee9a --- /dev/null +++ b/solution/3300-3399/3361.Shift Distance Between Two Strings/Solution.ts @@ -0,0 +1,19 @@ +function shiftDistance(s: string, t: string, nextCost: number[], previousCost: number[]): number { + const m = 26; + const s1: number[] = Array((m << 1) + 1).fill(0); + const s2: number[] = Array((m << 1) + 1).fill(0); + for (let i = 0; i < m << 1; i++) { + s1[i + 1] = s1[i] + nextCost[i % m]; + s2[i + 1] = s2[i] + previousCost[(i + 1) % m]; + } + let ans = 0; + const a = 'a'.charCodeAt(0); + for (let i = 0; i < s.length; i++) { + const x = s.charCodeAt(i) - a; + const y = t.charCodeAt(i) - a; + const c1 = s1[y + (y < x ? m : 0)] - s1[x]; + const c2 = s2[x + (x < y ? m : 0)] - s2[y]; + ans += Math.min(c1, c2); + } + return ans; +}