競賽程式設計簡單

特里樹實現

“Trie 實作”問題的詳細指南和 Python 實作。

問題陳述

簡單

寫一個模擬Trie運算的函數run_trie_operations(commands, arguments)。該函數接受命令字串列表和參數列表列表,按順序執行它們並返回執行結果列表。命令是:

- 'Trie':初始化 Trie(回傳 None

- 'insert':將字串插入 Trie(傳回 None

- 'search':如果字串在 Trie 中,則傳回 True,否則傳回 False

- 'startsWith':如果 Trie 中存在以給定前綴開頭的單字,則傳回 True,否則傳回 False

約束條件
  • 1 <= len(commands) <= 1000
  • 1 <= len(arguments[i]) <= 1
  • All words and prefixes consist of lowercase English letters.

範例

Example 1
Input
run_trie_operations(['Trie', 'insert', 'search', 'startsWith', 'insert', 'search'], [[], ['apple'], ['apple'], ['app'], ['app'], ['app']])
Output
[None, None, True, True, None, True]
Explanation

1. Initialize Trie -> None 2. Insert 'apple' -> None 3. Search 'apple' -> True 4. StartsWith 'app' -> True 5. Insert 'app' -> None 6. Search 'app' -> True

Need a Hint?
考慮使用 Trie 特定的資料結構,例如集合或堆疊。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。