Programowanie konkurencyjneŁatwe

Książka telefoniczna

Szczegółowy przewodnik i implementacja Python dla problemu „Książka telefoniczna”.

Oświadczenie o problemie

Łatwe

Napisz funkcję phone_directory(contacts, query), która pobiera listę unikalnych ciągów znaków contacts i ciąg zapytania query. Powinien zwrócić listę list ciągów znaków, gdzie i lista zawiera posortowane kontakty pasujące do przedrostka query do długości i+1 (indeks 1). Jeśli żaden kontakt nie pasuje, zwróć pustą listę dla tego prefiksu.

Ograniczenia
  • 1 <= len(contacts) <= 100
  • 1 <= len(query) <= 20
  • All strings consist of lowercase English letters.

Przykłady

Example 1
Input
phone_directory(['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], 'gee')
Output
[['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], ['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky'], ['geeikist', 'geeksforgeeks', 'geeksfortest', 'geeky']]
Explanation

For 'g', 'ge', and 'gee', all 4 contacts match prefix and are returned in sorted order.

Example 2
Input
phone_directory(['mobile', 'mouse', 'moneypot', 'monitor', 'mousepad'], 'mouse')
Output
[['mobile', 'monitor', 'moneypot', 'mouse', 'mousepad'], ['mobile', 'monitor', 'moneypot', 'mouse', 'mousepad'], ['mouse', 'mousepad'], ['mouse', 'mousepad'], ['mouse', 'mousepad']]
Explanation

For prefix 'm' and 'mo', all contacts match. For 'mou', 'mous', and 'mouse', only 'mouse' and 'mousepad' match.

Need a Hint?
Rozważ użycie specyficznych dla Trie struktur danych, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.