150強訪談簡單

編碼和解碼字串

「編碼和解碼字串」問題的詳細指南和 Python 實作。

問題陳述

簡單

設計一種演算法將字串列表編碼為單一字串。然後將編碼後的字串解碼回原始字串清單。

實現兩個功能:

- encode(strs: List[str]) -> str — 將字串清單編碼為單一字串。

- decode(s: str) -> List[str] — 將單一字串解碼回原始字串清單。

編碼字串應該能夠處理輸入字串中任何可能的字符,包括特殊字符和空字串。

約束條件
  • 0 <= len(strs) <= 200
  • 0 <= len(strs[i]) <= 200
  • strs[i] can contain any possible characters (0-255)

範例

Example 1
Input
strs = ["hello", "world"]
Output
["hello", "world"]
Explanation

The list is encoded into a single string and then decoded back to the original list ["hello", "world"].

Example 2
Input
strs = [""]
Output
[""]
Explanation

A list containing a single empty string is encoded and decoded correctly.

Example 3
Input
strs = ["we", "say", ":", "yes"]
Output
["we", "say", ":", "yes"]
Explanation

Special characters like ":" are handled correctly during encoding and decoding.

Need a Hint?
考慮使用數組和哈希特定的資料結構,例如集合或堆。
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 資源

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