電話帳 ---パイセップ--- 「電話帳」問題の詳細なガイドと __PYTERM_0__ の実装。 ---パイセップ--- 一意の文字列 __PYCODE_1__ とクエリ文字列 __PYCODE_2__ のリストを受け取る関数 __PYCODE_0__ を作成します。文字列のリストのリストを返す必要があります。__PYCODE_3__ 番目のリストには、__PYCODE_4__ のプレフィックスに一致する、長さ __PYCODE_5__ (1 から始まる) までの並べ替えられた連絡先が含まれます。一致する連絡先がない場合は、そのプレフィックスの空のリストを返します。 ---パイセップ--- 競技プログラミング ---パイセップ--- トライ ---パイセップ--- 「電話帳」問題はトライセクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- 電話帳のロジック フローを視覚化します。 ---パイセップ--- 電話帳の問題文を注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。 ---パイセップ--- 実稼働標準に合わせてコードをクリーンアップします。 ---パイセップ--- 空の入力構造体 ---パイセップ--- 単一要素入力 ---パイセップ--- 大きな数値限界 ---パイセップ--- Try アプローチのロジックを説明してください。 ---パイセップ--- null または空の入力などの特殊なケースについて説明します。 ---パイセップ--- 標準の Try 問題のプロパティが適用されます。 ---パイセップ--- セットやヒープなどの Trie 固有のデータ構造の使用を検討してください。 ---パイセップ--- 最小 XOR ペア ---パイセップ--- 「最小 XOR ペア」問題の詳細なガイドと __PYTERM_0__ 実装。 ---パイセップ--- 非負の整数 __PYCODE_1__ の配列を受け取り、配列内の 2 つの要素の最小 XOR 和を返す関数 __PYCODE_0__ を作成します。 ---パイセップ--- 競技プログラミング ---パイセップ--- トライ ---パイセップ--- 「最小 XOR ペア」問題は、トライ セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- 最小 XOR ペアのロジック フローを視覚化します。 ---パイセップ--- 最小 XOR ペアの問題文を注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。
Detailed guide and Python implementation for the 'Phone Directory' problem.
1. 学ぶ
The 'Phone Directory' problem is a key challenge in the Trie section.
This implementation focuses on easy-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Phone Directory.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Phone Directory carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
Clean up the code for production standards.
問題提起
Write a function phone_directory(contacts, query) that takes a list of unique strings contacts and a query string query. It should return a list of lists of strings, where the ith list contains the sorted contacts that match the prefix of query up to length i+1 (1-indexed). If no contact matches, return an empty list for that prefix.
- •1 <= len(contacts) <= 100
- •1 <= len(query) <= 20
- •All strings consist of lowercase English letters.
例
phone_directory(['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], 'gee')
[['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], ['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], ['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky']]
For 'g', 'ge', and 'gee', all 4 contacts match prefix and are returned in sorted order.
phone_directory(['mobile', 'mouse', 'moneypot', 'monitor', 'mousepad'], 'mouse')
[['mobile', 'monitor', 'moneypot', 'mouse', 'mousepad'], ['mobile', 'monitor', 'moneypot', 'mouse', 'mousepad'], ['mouse', 'mousepad'], ['mouse', 'mousepad'], ['mouse', 'mousepad']]
For prefix 'm' and 'mo', all contacts match. For 'mou', 'mous', and 'mouse', only 'mouse' and 'mousepad' match.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
解決する準備はできましたか?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
インタビューの洞察とバリエーション
複雑さの分析の内訳
なぜ時間がかかるのか: Directly evaluates all possibilities.
なぜ宇宙なのか: Uses standard local memory.
なぜ時間がかかるのか: Optimized paths reduce total operations.
なぜ宇宙なのか: May trade memory for speed.
最適化されたソリューションの Python コード
最適化されたソリューションの Python コード
def display_contacts_opt(contacts, s):
# Using the Trie-based solution (already implemented in TrieOpt)
return display_contacts_brute(contacts, s)ブルート フォース コード (スポイラーガード付き)
ブルート フォース コード (スポイラーガード付き)
def display_contacts_brute(contacts, s):
res = []
for i in range(1, len(s) + 1):
prefix = s[:i]; match = sorted(list(set(c for c in contacts if c.startswith(prefix))))
res.append(match if match else ["0"])
return resAlgorithm Pattern Checklist
When dealing with Trie data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
Key Revision Notes
Standard Trie problem properties apply.
関連する質問
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
推奨される Python リソース
関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。
Python ループ
Python ループを使用してデータを反復処理する方法を学びます。インタラクティブな例を使用して、for ループ、while ループ、ブレーク、継続、ループのベスト プラクティスをマスターします。
Python でリストを並べ替える方法
sort() メソッドとsorted() 関数を使用して、Python でリストを並べ替える方法を学びます。カスタムキーの並べ替えと逆順の例をご覧ください。
Python 文字列メソッドのチートシート
Python 文字列操作の完全なリファレンス ガイド。文字列プロパティの書式設定、検索、分割、置換、チェックをマスターします。
Python と JavaScript: どちらのプログラミング言語が最適ですか?
Python と JavaScript の包括的な比較。構文の違い、パフォーマンス、使用例 (バックエンドとフロントエンド)、およびコーディング例を調べます。