Le migliori 150 intervisteFacile

Ricerca di parole II

Guida dettagliata e implementazione Python per il problema 'Word Search II'.

Dichiarazione del problema

Facile

Data una scheda di caratteri m x n e un elenco di parole stringhe, restituisce tutte le parole sulla scheda.

Ogni parola deve essere costruita da lettere di celle adiacenti in sequenza, dove le celle adiacenti sono vicine orizzontalmente o verticalmente. La stessa cella di lettere non può essere utilizzata più di una volta in una parola.

Scrivi una funzione findWords(board: List[List[str]], words: List[str]) -> List[str].

Vincoli
  • m == len(board)
  • n == len(board[i])
  • 1 <= m, n <= 12
  • board[i][j] is a lowercase English letter
  • 1 <= len(words) <= 3 * 10^4
  • 1 <= len(words[i]) <= 10
  • words consist of lowercase English letters
  • All the strings in words are unique

Esempi

Example 1
Input
board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
Output
["oath","eat"]
Explanation

The words "oath" and "eat" can be found on the board. "pea" and "rain" cannot.

Example 2
Input
board = [["a","b"],["c","d"]], words = ["abcb"]
Output
[]
Explanation

The word "abcb" requires using the 'b' cell twice, which is invalid.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche per Trie come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.