Skip to content

Commit 9cab9d7

Browse files
authored
feat: add solutions to lc problem: No.3146 (doocs#3444)
No.3146.Permutation Difference between Two Strings
1 parent 8606546 commit 9cab9d7

File tree

6 files changed

+78
-14
lines changed

6 files changed

+78
-14
lines changed

solution/3100-3199/3145.Find Products of Elements of Big Array/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ tags:
114114

115115
### 方法一:二分查找 + 位运算
116116

117-
连续的正整数数字对应的强整数数组连接得到数组 $\textit{big\_nums}$,题目需要我们求出对于每个查询 $[\textit{left}, \textit{right}, \textit{mod}]$,子数组 $\textit{big\_nums}[\textit{left}..\textit{right}]$ 的乘积对 $\textit{mod}$ 取模的结果。由于子数组每个元素都是 $2$ 的幂,这等价于求子数组的幂次之和 $\textit{power}$,然后计算 $2^{\textit{power}} \bmod \textit{mod}$。例如,对于子数组 $[1, 4, 8]$,即 $[2^0, 2^2, 2^3]$,其幂次之和为 $0 + 2 + 3 = 5$,所以 $2^5 \bmod \textit{mod}$ 就是我们要求的结果。
117+
连续的正整数数字对应的强整数数组连接得到数组 $\textit{bignums}$,题目需要我们求出对于每个查询 $[\textit{left}, \textit{right}, \textit{mod}]$,子数组 $\textit{bignums}[\textit{left}..\textit{right}]$ 的乘积对 $\textit{mod}$ 取模的结果。由于子数组每个元素都是 $2$ 的幂,这等价于求子数组的幂次之和 $\textit{power}$,然后计算 $2^{\textit{power}} \bmod \textit{mod}$。例如,对于子数组 $[1, 4, 8]$,即 $[2^0, 2^2, 2^3]$,其幂次之和为 $0 + 2 + 3 = 5$,所以 $2^5 \bmod \textit{mod}$ 就是我们要求的结果。
118118

119-
因此,我们不妨将 $\textit{big\_nums}$ 转换为幂次数组,即对于子数组 $[1, 4, 8]$,我们将其转换为 $[0, 2, 3]$。这样,问题转换为求幂次数组的子数组之和,即 $\textit{power} = \textit{f}(\textit{right} + 1) - \textit{f}(\textit{left})$,其中 $\textit{f}(i)$ 表示 $\textit{big\_nums}[0..i)$ 的幂次之和,也即是前缀和。
119+
因此,我们不妨将 $\textit{bignums}$ 转换为幂次数组,即对于子数组 $[1, 4, 8]$,我们将其转换为 $[0, 2, 3]$。这样,问题转换为求幂次数组的子数组之和,即 $\textit{power} = \textit{f}(\textit{right} + 1) - \textit{f}(\textit{left})$,其中 $\textit{f}(i)$ 表示 $\textit{bignums}[0..i)$ 的幂次之和,也即是前缀和。
120120

121121
接下来,就是根据下标 $i$ 计算 $\textit{f}(i)$ 的值。我们可以使用二分查找的方法,先找到强数组长度和小于 $i$ 的最大数字,然后再计算剩下的数字的幂次之和。
122122

@@ -140,7 +140,7 @@ tags:
140140
| 13 | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">0</span> | <span style="color: yellow;">1</span> |
141141
| 14 | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">0</span> |
142142

143-
将数字按照 $[2^i, 2^{i+1}-1]$ 的区间划分为不同的颜色,可以发现,区间 $[2^i, 2^{i+1}-1]$ 的数字,相当于在区间 $[0, 2^i-1]$ 的数字基础上,每个数字加上 $2^i$。我们可以根据这个规律,计算出 $\textit{big\_nums}$ 的前 $i$ 组的所有数字的强数组个数之和 $\textit{cnt}[i]$ 和幂次之和 $\textit{s}[i]$。
143+
将数字按照 $[2^i, 2^{i+1}-1]$ 的区间划分为不同的颜色,可以发现,区间 $[2^i, 2^{i+1}-1]$ 的数字,相当于在区间 $[0, 2^i-1]$ 的数字基础上,每个数字加上 $2^i$。我们可以根据这个规律,计算出 $\textit{bignums}$ 的前 $i$ 组的所有数字的强数组个数之和 $\textit{cnt}[i]$ 和幂次之和 $\textit{s}[i]$。
144144

145145
接下来,对于任何数字,我们考虑如何计算其强数组的个数和幂次之和。我们可以通过二进制的方式,从最高位开始,诸位计算。例如,对于数字 $13 = 2^3 + 2^2 + 2^0$,前 $2^3$ 个数字的结果可以由 $textit{cnt}[3]$ 和 $\textit{s}[3]$ 计算得到,而剩下的 $[2^3, 13]$ 的结果,相当于给 $[0, 13-2^3]$ 的所有数字,即 $[0, 5]$ 的所有数字的强数组增加 $3$,问题转换为计算 $[0, 5]$ 的所有数字的强数组的个数和幂次之和。这样,我们可以计算出任意数字的强数组的个数和幂次之和。
146146

solution/3100-3199/3145.Find Products of Elements of Big Array/README_EN.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ tags:
112112

113113
### Solution 1: Binary Search + Bit Manipulation
114114

115-
The continuous positive integer numbers correspond to the strong integer array, forming the array $\textit{big\_nums}$. The problem requires us to find the result of the product of the subarray $\textit{big\_nums}[\textit{left}..\textit{right}]$ modulo $\textit{mod}$ for each query $[\textit{left}, \textit{right}, \textit{mod}]$. Since each element of the subarray is a power of 2, this is equivalent to finding the sum of the powers $\textit{power}$ of the subarray, and then calculating $2^{\textit{power}} \bmod \textit{mod}$. For example, for the subarray $[1, 4, 8]$, i.e., $[2^0, 2^2, 2^3]$, the sum of the powers is $0 + 2 + 3 = 5$, so $2^5 \bmod \textit{mod}$ is the result we need.
115+
The continuous positive integer numbers correspond to the strong integer array, forming the array $\textit{bignums}$. The problem requires us to find the result of the product of the subarray $\textit{bignums}[\textit{left}..\textit{right}]$ modulo $\textit{mod}$ for each query $[\textit{left}, \textit{right}, \textit{mod}]$. Since each element of the subarray is a power of 2, this is equivalent to finding the sum of the powers $\textit{power}$ of the subarray, and then calculating $2^{\textit{power}} \bmod \textit{mod}$. For example, for the subarray $[1, 4, 8]$, i.e., $[2^0, 2^2, 2^3]$, the sum of the powers is $0 + 2 + 3 = 5$, so $2^5 \bmod \textit{mod}$ is the result we need.
116116

117-
Therefore, we can convert $\textit{big\_nums}$ into an array of powers. For example, for the subarray $[1, 4, 8]$, we convert it to $[0, 2, 3]$. Thus, the problem is transformed into finding the sum of the subarray of powers, i.e., $\textit{power} = \textit{f}(\textit{right} + 1) - \textit{f}(\textit{left})$, where $\textit{f}(i)$ represents the sum of the powers of $\textit{big\_nums}[0..i)$, which is the prefix sum.
117+
Therefore, we can convert $\textit{bignums}$ into an array of powers. For example, for the subarray $[1, 4, 8]$, we convert it to $[0, 2, 3]$. Thus, the problem is transformed into finding the sum of the subarray of powers, i.e., $\textit{power} = \textit{f}(\textit{right} + 1) - \textit{f}(\textit{left})$, where $\textit{f}(i)$ represents the sum of the powers of $\textit{bignums}[0..i)$, which is the prefix sum.
118118

119119
Next, we calculate the value of $\textit{f}(i)$ based on the index $i$. We can use binary search to find the largest number whose strong array length is less than $i$, and then calculate the sum of the powers of the remaining numbers.
120120

@@ -138,7 +138,7 @@ According to the problem description, we list the strong integers for numbers $0
138138
| 13 | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">0</span> | <span style="color: yellow;">1</span> |
139139
| 14 | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">1</span> | <span style="color: yellow;">0</span> |
140140

141-
By dividing the numbers into different colors according to the interval $[2^i, 2^{i+1}-1]$, we can see that the numbers in the interval $[2^i, 2^{i+1}-1]$ are equivalent to adding $2^i$ to each number in the interval $[0, 2^i-1]$. Based on this pattern, we can calculate the total number of strong arrays $\textit{cnt}[i]$ and the sum of powers $\textit{s}[i]$ for the first $i$ groups of numbers in $\textit{big\_nums}$.
141+
By dividing the numbers into different colors according to the interval $[2^i, 2^{i+1}-1]$, we can see that the numbers in the interval $[2^i, 2^{i+1}-1]$ are equivalent to adding $2^i$ to each number in the interval $[0, 2^i-1]$. Based on this pattern, we can calculate the total number of strong arrays $\textit{cnt}[i]$ and the sum of powers $\textit{s}[i]$ for the first $i$ groups of numbers in $\textit{bignums}$.
142142

143143
Next, for any number, we consider how to calculate the number of strong arrays and the sum of powers. We can use the binary method, calculating from the highest bit. For example, for the number $13 = 2^3 + 2^2 + 2^0$, the result of the first $2^3$ numbers can be obtained from $\textit{cnt}[3]$ and $\textit{s}[3]$, and the result of the remaining $[2^3, 13]$ is equivalent to adding $3$ to all numbers in $[0, 13-2^3]$, i.e., $[0, 5]$. The problem is transformed into calculating the number of strong arrays and the sum of powers for $[0, 5]$. In this way, we can calculate the number of strong arrays and the sum of powers for any number.
144144

solution/3100-3199/3146.Permutation Difference between Two Strings/README.md

+28-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ tags:
7474

7575
<!-- solution:start -->
7676

77-
### 方法一
77+
### 方法一:哈希表或数组
78+
79+
我们可以使用哈希表或者一个长度为 $26$ 的数组 $\textit{d}$ 来存储字符串 $\textit{s}$ 中每个字符的位置。
80+
81+
然后遍历字符串 $\textit{t}$,计算每个字符在字符串 $\textit{t}$ 中的位置与在字符串 $\textit{s}$ 中的位置之差的绝对值之和即可。
82+
83+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{s}$ 的长度。空间复杂度 $O(|\Sigma|)$,其中 $\Sigma$ 为字符集,这里是小写英文字母,所以 $|\Sigma| \leq 26$。
7884

7985
<!-- tabs:start -->
8086

@@ -148,16 +154,35 @@ function findPermutationDifference(s: string, t: string): number {
148154
const d: number[] = Array(26).fill(0);
149155
const n = s.length;
150156
for (let i = 0; i < n; ++i) {
151-
d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
157+
d[s.charCodeAt(i) - 97] = i;
152158
}
153159
let ans = 0;
154160
for (let i = 0; i < n; ++i) {
155-
ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i);
161+
ans += Math.abs(d[t.charCodeAt(i) - 97] - i);
156162
}
157163
return ans;
158164
}
159165
```
160166

167+
#### C#
168+
169+
```cs
170+
public class Solution {
171+
public int FindPermutationDifference(string s, string t) {
172+
int[] d = new int[26];
173+
int n = s.Length;
174+
for (int i = 0; i < n; ++i) {
175+
d[s[i] - 'a'] = i;
176+
}
177+
int ans = 0;
178+
for (int i = 0; i < n; ++i) {
179+
ans += Math.Abs(d[t[i] - 'a'] - i);
180+
}
181+
return ans;
182+
}
183+
}
184+
```
185+
161186
<!-- tabs:end -->
162187

163188
<!-- solution:end -->

solution/3100-3199/3146.Permutation Difference between Two Strings/README_EN.md

+28-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ tags:
7272

7373
<!-- solution:start -->
7474

75-
### Solution 1
75+
### Solution 1: Hash Table or Array
76+
77+
We can use a hash table or an array of length $26$, denoted as $\textit{d}$, to store the positions of each character in the string $\textit{s}$.
78+
79+
Then, we traverse the string $\textit{t}$ and calculate the sum of the absolute differences between the positions of each character in the string $\textit{t}$ and the positions in the string $\textit{s}$.
80+
81+
The time complexity is $O(n)$, where $n$ is the length of the string $\textit{s}$. The space complexity is $O(|\Sigma|)$, where $\Sigma$ is the character set. Here, it is lowercase English letters, so $|\Sigma| \leq 26$.
7682

7783
<!-- tabs:start -->
7884

@@ -146,16 +152,35 @@ function findPermutationDifference(s: string, t: string): number {
146152
const d: number[] = Array(26).fill(0);
147153
const n = s.length;
148154
for (let i = 0; i < n; ++i) {
149-
d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
155+
d[s.charCodeAt(i) - 97] = i;
150156
}
151157
let ans = 0;
152158
for (let i = 0; i < n; ++i) {
153-
ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i);
159+
ans += Math.abs(d[t.charCodeAt(i) - 97] - i);
154160
}
155161
return ans;
156162
}
157163
```
158164

165+
#### C#
166+
167+
```cs
168+
public class Solution {
169+
public int FindPermutationDifference(string s, string t) {
170+
int[] d = new int[26];
171+
int n = s.Length;
172+
for (int i = 0; i < n; ++i) {
173+
d[s[i] - 'a'] = i;
174+
}
175+
int ans = 0;
176+
for (int i = 0; i < n; ++i) {
177+
ans += Math.Abs(d[t[i] - 'a'] - i);
178+
}
179+
return ans;
180+
}
181+
}
182+
```
183+
159184
<!-- tabs:end -->
160185

161186
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Solution {
2+
public int FindPermutationDifference(string s, string t) {
3+
int[] d = new int[26];
4+
int n = s.Length;
5+
for (int i = 0; i < n; ++i) {
6+
d[s[i] - 'a'] = i;
7+
}
8+
int ans = 0;
9+
for (int i = 0; i < n; ++i) {
10+
ans += Math.Abs(d[t[i] - 'a'] - i);
11+
}
12+
return ans;
13+
}
14+
}

solution/3100-3199/3146.Permutation Difference between Two Strings/Solution.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ function findPermutationDifference(s: string, t: string): number {
22
const d: number[] = Array(26).fill(0);
33
const n = s.length;
44
for (let i = 0; i < n; ++i) {
5-
d[s.charCodeAt(i) - 'a'.charCodeAt(0)] = i;
5+
d[s.charCodeAt(i) - 97] = i;
66
}
77
let ans = 0;
88
for (let i = 0; i < n; ++i) {
9-
ans += Math.abs(d[t.charCodeAt(i) - 'a'.charCodeAt(0)] - i);
9+
ans += Math.abs(d[t.charCodeAt(i) - 97] - i);
1010
}
1111
return ans;
1212
}

0 commit comments

Comments
 (0)