Skip to content

Commit b1fd7e5

Browse files
committed
+ problem 2451
1 parent 1237cde commit b1fd7e5

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 2451. Odd String Difference
2+
You are given an array of equal-length strings `words`. Assume that the length of each string is `n`.
3+
4+
Each string `words[i]` can be converted into a **difference integer array** `difference[i]` of length `n - 1` where `difference[i][j] = words[i][j+1] - words[i][j]` where `0 <= j <= n - 2`. Note that the difference between two letters is the difference between their **positions** in the alphabet i.e. the position of `'a'` is `0`, `'b'` is `1`, and `'z'` is `25`.
5+
6+
* For example, for the string `"acb"`, the difference integer array is `[2 - 0, 1 - 2] = [2, -1]`.
7+
8+
All the strings in words have the same difference integer array, **except one**. You should find that string.
9+
10+
Return *the string in* `words` *that has different **difference integer array***.
11+
12+
#### Example 1:
13+
<pre>
14+
<strong>Input:</strong> words = ["adc","wzy","abc"]
15+
<strong>Output:</strong> "abc"
16+
<strong>Explanation:</strong>
17+
- The difference integer array of "adc" is [3 - 0, 2 - 3] = [3, -1].
18+
- The difference integer array of "wzy" is [25 - 22, 24 - 25]= [3, -1].
19+
- The difference integer array of "abc" is [1 - 0, 2 - 1] = [1, 1].
20+
The odd array out is [1, 1], so we return the corresponding string, "abc".
21+
</pre>
22+
23+
#### Example 2:
24+
<pre>
25+
<strong>Input:</strong> words = ["aaa","bob","ccc","ddd"]
26+
<strong>Output:</strong> "bob"
27+
<strong>Explanation:</strong> All the integer arrays are [0, 0] except for "bob", which corresponds to [13, -13].
28+
</pre>
29+
30+
#### Constraints:
31+
* `3 <= words.length <= 100`
32+
* `n == words[i].length`
33+
* `2 <= n <= 20`
34+
* `words[i]` consists of lowercase English letters.
35+
36+
## Solutions (Python)
37+
38+
### 1. Solution
39+
```Python
40+
class Solution:
41+
def oddString(self, words: List[str]) -> str:
42+
n = len(words[0])
43+
difference = []
44+
45+
for i in range(len(words)):
46+
difference.append([])
47+
for j in range(n - 1):
48+
difference[i].append(ord(words[i][j + 1]) - ord(words[i][j]))
49+
50+
if i >= 2:
51+
prev = difference[i - 1] == difference[i - 2]
52+
curr = difference[i] == difference[i - 1]
53+
if prev and not curr:
54+
return words[i]
55+
elif not prev and curr:
56+
return words[i - 2]
57+
elif not prev and not curr:
58+
return words[i - 1]
59+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# 2451. 差值数组不同的字符串
2+
给你一个字符串数组 `words` ,每一个字符串长度都相同,令所有字符串的长度都为 `n`
3+
4+
每个字符串 `words[i]` 可以被转化为一个长度为 `n - 1`**差值整数数组** `difference[i]` ,其中对于 `0 <= j <= n - 2``difference[i][j] = words[i][j+1] - words[i][j]` 。注意两个字母的差值定义为它们在字母表中 **位置** 之差,也就是说 `'a'` 的位置是 `0``'b'` 的位置是 `1``'z'` 的位置是 `25`
5+
6+
* 比方说,字符串 `"acb"` 的差值整数数组是 `[2 - 0, 1 - 2] = [2, -1]`
7+
8+
`words` 中所有字符串 **除了一个字符串以外** ,其他字符串的差值整数数组都相同。你需要找到那个不同的字符串。
9+
10+
请你返回 `words`**差值整数数组** 不同的字符串。
11+
12+
#### 示例 1:
13+
<pre>
14+
<strong>输入:</strong> words = ["adc","wzy","abc"]
15+
<strong>输出:</strong> "abc"
16+
<strong>解释:</strong>
17+
- "adc" 的差值整数数组是 [3 - 0, 2 - 3] = [3, -1] 。
18+
- "wzy" 的差值整数数组是 [25 - 22, 24 - 25]= [3, -1] 。
19+
- "abc" 的差值整数数组是 [1 - 0, 2 - 1] = [1, 1] 。
20+
不同的数组是 [1, 1],所以返回对应的字符串,"abc"。
21+
</pre>
22+
23+
#### 示例 2:
24+
<pre>
25+
<strong>输入:</strong> words = ["aaa","bob","ccc","ddd"]
26+
<strong>输出:</strong> "bob"
27+
<strong>解释:</strong> 除了 "bob" 的差值整数数组是 [13, -13] 以外,其他字符串的差值整数数组都是 [0, 0] 。
28+
</pre>
29+
30+
#### 提示:
31+
* `3 <= words.length <= 100`
32+
* `n == words[i].length`
33+
* `2 <= n <= 20`
34+
* `words[i]` 只含有小写英文字母。
35+
36+
## 题解 (Python)
37+
38+
### 1. 题解
39+
```Python
40+
class Solution:
41+
def oddString(self, words: List[str]) -> str:
42+
n = len(words[0])
43+
difference = []
44+
45+
for i in range(len(words)):
46+
difference.append([])
47+
for j in range(n - 1):
48+
difference[i].append(ord(words[i][j + 1]) - ord(words[i][j]))
49+
50+
if i >= 2:
51+
prev = difference[i - 1] == difference[i - 2]
52+
curr = difference[i] == difference[i - 1]
53+
if prev and not curr:
54+
return words[i]
55+
elif not prev and curr:
56+
return words[i - 2]
57+
elif not prev and not curr:
58+
return words[i - 1]
59+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def oddString(self, words: List[str]) -> str:
3+
n = len(words[0])
4+
difference = []
5+
6+
for i in range(len(words)):
7+
difference.append([])
8+
for j in range(n - 1):
9+
difference[i].append(ord(words[i][j + 1]) - ord(words[i][j]))
10+
11+
if i >= 2:
12+
prev = difference[i - 1] == difference[i - 2]
13+
curr = difference[i] == difference[i - 1]
14+
if prev and not curr:
15+
return words[i]
16+
elif not prev and curr:
17+
return words[i - 2]
18+
elif not prev and not curr:
19+
return words[i - 1]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@
11071107
[2442][2442l]|[Count Number of Distinct Integers After Reverse Operations][2442] |![py]
11081108
[2443][2443l]|[Sum of Number and Its Reverse][2443] |![py]
11091109
[2446][2446l]|[Determine if Two Events Have Conflict][2446] |![py]
1110+
[2451][2451l]|[Odd String Difference][2451] |![py]
11101111
[2453][2453l]|[Destroy Sequential Targets][2453] |![rs]
11111112
[2455][2455l]|[Average Value of Even Numbers That Are Divisible by Three][2455] |![rs]
11121113
[2465][2465l]|[Number of Distinct Averages][2465] |![rs]
@@ -2244,6 +2245,7 @@
22442245
[2442]:./Medium/2442-Count%20Number%20of%20Distinct%20Integers%20After%20Reverse%20Operations/README.md#2442-count-number-of-distinct-integers-after-reverse-operations
22452246
[2443]:./Medium/2443-Sum%20of%20Number%20and%20Its%20Reverse/README.md#2443-sum-of-number-and-its-reverse
22462247
[2446]:./Easy/2446-Determine%20if%20Two%20Events%20Have%20Conflict/README.md#2446-determine-if-two-events-have-conflict
2248+
[2451]:./Easy/2451-Odd%20String%20Difference/README.md#2451-odd-string-difference
22472249
[2453]:./Medium/2453-Destroy%20Sequential%20Targets/README.md#2453-destroy-sequential-targets
22482250
[2455]:./Easy/2455-Average%20Value%20of%20Even%20Numbers%20That%20Are%20Divisible%20by%20Three/README.md#2455-average-value-of-even-numbers-that-are-divisible-by-three
22492251
[2465]:./Easy/2465-Number%20of%20Distinct%20Averages/README.md#2465-number-of-distinct-averages
@@ -3386,6 +3388,7 @@
33863388
[2442l]:https://leetcode.com/problems/count-number-of-distinct-integers-after-reverse-operations/
33873389
[2443l]:https://leetcode.com/problems/sum-of-number-and-its-reverse/
33883390
[2446l]:https://leetcode.com/problems/determine-if-two-events-have-conflict/
3391+
[2451l]:https://leetcode.com/problems/odd-string-difference/
33893392
[2453l]:https://leetcode.com/problems/destroy-sequential-targets/
33903393
[2455l]:https://leetcode.com/problems/average-value-of-even-numbers-that-are-divisible-by-three/
33913394
[2465l]:https://leetcode.com/problems/number-of-distinct-averages/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@
11071107
[2442][2442l]|[反转之后不同整数的数目][2442] |![py]
11081108
[2443][2443l]|[反转之后的数字和][2443] |![py]
11091109
[2446][2446l]|[判断两个事件是否存在冲突][2446] |![py]
1110+
[2451][2451l]|[差值数组不同的字符串][2451] |![py]
11101111
[2453][2453l]|[摧毁一系列目标][2453] |![rs]
11111112
[2455][2455l]|[可被三整除的偶数的平均值][2455] |![rs]
11121113
[2465][2465l]|[不同的平均值数目][2465] |![rs]
@@ -2244,6 +2245,7 @@
22442245
[2442]:./Medium/2442-Count%20Number%20of%20Distinct%20Integers%20After%20Reverse%20Operations/README_CN.md#2442-反转之后不同整数的数目
22452246
[2443]:./Medium/2443-Sum%20of%20Number%20and%20Its%20Reverse/README_CN.md#2443-反转之后的数字和
22462247
[2446]:./Easy/2446-Determine%20if%20Two%20Events%20Have%20Conflict/README_CN.md#2446-判断两个事件是否存在冲突
2248+
[2451]:./Easy/2451-Odd%20String%20Difference/README_CN.md#2451-差值数组不同的字符串
22472249
[2453]:./Medium/2453-Destroy%20Sequential%20Targets/README_CN.md#2453-摧毁一系列目标
22482250
[2455]:./Easy/2455-Average%20Value%20of%20Even%20Numbers%20That%20Are%20Divisible%20by%20Three/README_CN.md#2455-可被三整除的偶数的平均值
22492251
[2465]:./Easy/2465-Number%20of%20Distinct%20Averages/README_CN.md#2465-不同的平均值数目
@@ -3386,6 +3388,7 @@
33863388
[2442l]:https://leetcode.cn/problems/count-number-of-distinct-integers-after-reverse-operations/
33873389
[2443l]:https://leetcode.cn/problems/sum-of-number-and-its-reverse/
33883390
[2446l]:https://leetcode.cn/problems/determine-if-two-events-have-conflict/
3391+
[2451l]:https://leetcode.cn/problems/odd-string-difference/
33893392
[2453l]:https://leetcode.cn/problems/destroy-sequential-targets/
33903393
[2455l]:https://leetcode.cn/problems/average-value-of-even-numbers-that-are-divisible-by-three/
33913394
[2465l]:https://leetcode.cn/problems/number-of-distinct-averages/

0 commit comments

Comments
 (0)