Leetcode - search-suggestions-system
https://leetcode.com/problems/search-suggestions-system/
Given an array of strings products
and a string searchWord
. We want to design a system that suggests at most three product names from products
after each character of searchWord
is typed. Suggested products should have common prefix with the searchWord. If there are more than three products with a common prefix return the three lexicographically minimums products.
Return list of lists of the suggested products
after each character of searchWord
is typed.
Input: products = ["bags","baggage","banner","box","cloths"], searchWord = "bags" Output: [["baggage","bags","banner"],["baggage","bags","banner"],["baggage","bags"],["bags"]]
The solution is using trie. Build a trie for products, store each product in the leaf. After that iterate searchWord and find the leaf node for each byte in searchWord. For this question, it only requires 3 results, just set limitation for the length of result.
func suggestedProducts(products []string, searchWord string) [][]string { root := buildTrie(products) node := root final := [][]string{} for i := 0; i < len(searchWord); i++ { index := searchWord[i] - 'a' result := []string{} if node != nil { node = node.children[index] recursive(node, &result) } final = append(final, result) } return final } type TreeNode struct { value string children []*TreeNode } func buildTrie(products []string) *TreeNode { root := &TreeNode{ children: make([]*TreeNode, 26), } node := root for _, p := range products { node = root for _, b := range p { index := byte(b) - 'a' if node.children[index] != nil { node = node.children[index] continue } node.children[index] = &TreeNode{ children: make([]*TreeNode, 26), } node = node.children[index] } node.value = p } return root } func recursive(root *TreeNode, result *[]string) { if len(*result) == 3 { return } if root == nil { return } if root.value != "" { *result = append(*result, root.value) } for i := 0; i < 26; i++ { if root.children[i] != nil { recursive(root.children[i], result) } } } func levelOrder(root *TreeNode) { queue := []*TreeNode{root} for len(queue) != 0 { size := len(queue) for i := 0; i < size; i++ { node := queue[i] for _, c := range node.children { if c != nil { queue = append(queue, c) } } } queue = queue[size:] } }
# @param {String[]} products # @param {String} search_word # @return {String[][]} def suggested_products(products, search_word) root = build_trie(products) node = root final = [] for i in 0..search_word.length-1 index = search_word[i].ord - 'a'.ord result = [] if node != nil node = node.children[index] recursive(node, result) end final << result end return final end class TreeNode attr_accessor :val, :children def initialize(val) @val = val @children = Array.new(26) end end def build_trie(products) root = TreeNode.new("") node = root products.each do |product| node = root product.each_char do |p| index = p.ord - 'a'.ord if node.children[index].nil? node.children[index] = TreeNode.new("") end node = node.children[index] end node.val = product end return root end def recursive(root, result) if result.length == 3 return end if root == nil return end if root.val != "" result << root.val end for i in 0..25 if root.children[i] != nil recursive(root.children[i], result) end end end
Aug 25, 2021 12:25:00 AM
This algorithm is quite good. It will be good to use practically.
Aug 25, 2021 12:27:40 AM
New Coding techniques are being used in this websites to give ideas to the student.
Aug 25, 2021 12:31:11 AM
It has applied all new types of coding techniques and easy ways to solve problems.
Sep 10, 2021 06:08:57 PM
your site is beautiful in the world.
Sep 15, 2021 05:44:59 PM
I found this one pretty enthralling, and you should go into my get-together also.
Haridwar Escorts
Escorts in Roorkee
Haldwani Escorts Service
Escorts Service in Chennai
Rishikesh Escort
We give math homework help associations beginning with one side of the planet then onto the going with. I'm stunned with your article. We like that obligingly keep on making more substance.
Sep 15, 2021 05:46:30 PM
In this life, nobody yet you can see the worth in your best.
Mussoorie Escorts
Escorts in Rudrapur
Nainital Escorts Service
Escorts Service in Jim Corbett National Park
Mall Road Mussoorie Escort
So examine horrendousness and be sensitive with please. Suffering is a compact surrendered aftermath of my mind alone.
Sep 15, 2021 05:48:21 PM
I'm taking a gander at how I may be told at whatever point another post has been made Dehradun Call Girl Service.
Prem Nagar Escorts
Escorts in Selaqui
Ramnagar Escorts Service
Escorts Service in Daman
Ahmedabad Escort
I have become restricted with your RSS which may work? Have a confounding day.
Sep 15, 2021 05:50:01 PM
I'm enterprisingly searching for some free stuffs over the web Dehradun Call Girls Service.
Dharamshala Escorts
Escorts in Kasol
Mohali Escorts Service
Escorts Service in Rajpur Road
Shivpuri Escort
there are other than a couple of affiliations that give free models.
Sep 15, 2021 05:51:53 PM
It's overall clear and everything thought about mindful Call Girl in Dehradun.
Patna Escorts
Escorts in Navi Mumbai
Surat Escorts Service
Escorts Service in Mcleodganj
Shimla Escort
You have even seen how to make it reasonable and simple to look at. You have some ensured creation limit.
Sep 15, 2021 05:53:20 PM
I legend reward a seeing space that experience giving a setting strong asset for set meandering.
Raipur Escorts
Escorts in Visakhapatnam
Kanpur Escorts Service
Escorts Service in Bhopal
Nagpur Escort
it's far the old what goes on the designs for appears as per an overall perspective concerning pushing.
Sep 15, 2021 05:54:50 PM
Awesome information, an obligation of appreciation is all together for sharing.
Agra Escorts
Escorts in Mumbai
Escorts Service in Vizag
Varanasi Escort
This condo suite data for a dating site people are loathed in his life. With the help of this site, you can find your best dating young lady.
Sep 28, 2021 06:12:38 PM
I legend reward a seeing space that experience giving a setting solid resource for set wandering. Independent Mumbai Escorts it's far the old what goes on the plans for shows up as shown by a general point of view concerning pushing.
Sep 05, 2022 07:32:20 PM
CG Board Model Paper 2023 Class 10 Pdf Download with Answers for English Medium, Hindi Medium, Urdu Medium & Students for Small Answers, Long Answer, Very Long Answer Questions, and Essay Type Questions to Term1 & Term2 Exams at official website. CGBSE Question Paper Class 10 New Exam Scheme or Question Pattern for Sammittive Assignment Exams (SA1 & SA2): Very Long Answer (VLA), Long Answer (LA), Small Answer (SA), Very Small Answer (VSA), Single Answer, Multiple Choice and etc.
Jul 25, 2023 11:38:02 PM
Mit WhatsApp Status können Sie Bilder und Videos an andere Benutzer der Messaging-App senden, die sich zu einer beliebten Social-Networking-Plattform in Indien entwickelt hat. whatsapp status speichern WhatsApp war früher einfach eine Messaging-App. WhatsApp Status ist eine Option, mit der Sie Statusaktualisierungen senden können, die nach einem Tag gelöscht werden. Sie können Bilder, Videos, Text, Links und animierte GIFs senden.
Jul 26, 2023 06:35:36 PM
Karnataka 8th Textbook 2024 will be Available in the Official Website Karnataka Textbook Society Upload the High School Standard eBooks, KSEEB Books Prepare by Senior Experts. KTBS Provide Std Kannada, English, Hindi, Urdu, Marathi, Tamil and Telugu Medium Books for Pdf File is upload in Website.Karnataka Textbook Society (KTBS) Every Year Published and Distribution for 8th Textbook KSEEB 8th Class Books 2024 Karnataka State High School Academic Year Start in Month of Jun Books 2024 Available in English, Hindi, Kannada, Science, Social Science, Maths Textbook at your Provide High Head Master.Karnataka State Board High School Students or Teachers can easily Download. These books Provide Quality Education