Skip to content

Commit 93abed1

Browse files
committed
add 2273
1 parent fc1ff4f commit 93abed1

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

javascript/LeetCode/Array/2273.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* 2273. Find Resultant Array After Removing Anagrams
3+
*
4+
* @param {string[]} words
5+
* @return {string[]}
6+
*/
7+
var removeAnagrams = function(words) {
8+
let res = [];
9+
let prevStr = "";
10+
for(let i = 0;i < words.length;++i) {
11+
let s = words[i].split("").sort().join("");
12+
13+
if (s !== prevStr) {
14+
res.push(words[i]);
15+
prevStr = s;
16+
}
17+
}
18+
return res;
19+
};
20+
let w = ["abba","baba","bbaa","cd","cd"];
21+
// ["abba","cd"]
22+
console.log(removeAnagrams(w));

python/index.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,4 +264,5 @@ def replaceDigits(s: str) -> str:
264264
return res
265265
s = "a1c1e1"
266266
# Output: "abcdef"
267-
print(replaceDigits(s))
267+
print(replaceDigits(s))
268+

python/leetcode/list/2273.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
def removeAnagrams(words: List[str]) -> List[str]:
3+
'''
4+
2273. Find Resultant Array After Removing Anagrams
5+
在一次操作中,選擇任何一個索引值 i 使得 0 < i < words.length 且 words[i - 1] 與 words[i] 互相為易位構詞(Anagram),
6+
並將 words[i] 從 words 中刪除。只要你可以選擇滿足這些條件的索引值,持續執行此操作。
7+
回傳執行所有操作後的 words。可以證明在每一次操作以任意順序選擇這些索引值將得到相同的結果。
8+
9+
10+
直接掃過一次 words,只要遇到 words[i] 與 words[i - 1] 是易位構詞就把 words[i] 刪掉即可。
11+
'''
12+
res = []
13+
s = ""
14+
for i in range(0,len(words)):
15+
split = "".join(sorted(words[i]))
16+
if s != split:
17+
res.append(words[i])
18+
s = split
19+
20+
return res
21+
w = ["abba","baba","bbaa","cd","cd"]
22+
# ["abba","cd"]
23+
print(removeAnagrams(w))

0 commit comments

Comments
 (0)