題目出處
weekly-contest-282
2186. Minimum Number of Steps to Make Two Strings Anagram II
難度
Medium
題目分類
個人範例程式碼
class Solution:
def minSteps(self, s: str, t: str) -> int:
ans = len(s) + len(t)
for same_word in (set(s) & set(t)):
ans -= min(Counter(s)[same_word], Counter(t)[same_word])*2 # two side skip step
return ans
Time Complexity
O(n)
算法說明
最大步數為 len(s) + len(t),我們再去看要減少多少就好。
統計每個字元後,用 set 取「交集」,找出重複的字元數量
corner case 特殊情況處理
x
Boundary conditions/ Edge conditions 邊際情況處理
- 注意:因為兩組字母是雙向的減少步驟,因此在減少步數時「需要*2」
Reference
⭐ Leetcode 解題紀錄 ⭐ | 題型 | 資料結構 | Python Solution | C++ Solution | Note | |
---|---|---|---|---|---|---|
⭐BFS 相關題型 ⭐ | ||||||
104 | Maximum Depth of Binary Tree | BFS (分層) | Python | |||
94 | Binary Tree Inorder Traversal | BFS (分層) | Tree | Python | 內含 處理 Tree 樹問題的重點 | |
102 | Binary Tree Level Order Traversal | BFS (分層) | Tree | Python | ||
103 | Binary Tree Zigzag Level Order Traversal | BFS (分層) | Tree | Python | ||
107 | Binary Tree Level Order Traversal II | BFS (分層) | Tree | Python | ||
133 | Clone Graph | BFS (分層) | Graph | Python | Graph 的基本操作 #重要題型 | |
127 | Word Ladder | BFS (分層), DFS | Graph | Python | ||
[Lint] 127 | Topological Sorting | BFS (拓撲) | Python | 內有 indegree, outdegree 介紹 #重要題型 | ||
207 | Course Schedule | BFS (拓樸) | Graph | Python | ||
210 | Course Schedule II | BFS (拓樸) | Graph | Python | ||
[Lint] 892 | Alien Dictionary | BFS (拓樸) | Graph | Python | ||
[Lint] 431 | Connected Component in Undirected Graph | BFS (連通塊) | Graph | Python | 內含 BFS 模板 #重要題型 | |
1091 | Shortest Path in Binary Matrix | BFS (最短路徑) | Matrix | Python |