

{"id":159,"date":"2022-03-05T19:53:10","date_gmt":"2022-03-06T00:53:10","guid":{"rendered":"https:\/\/sites.temple.edu\/vahid\/?p=159"},"modified":"2022-03-05T19:53:11","modified_gmt":"2022-03-06T00:53:11","slug":"hackerrank-solutions-no-prefix-set","status":"publish","type":"post","link":"https:\/\/sites.temple.edu\/vahid\/2022\/03\/05\/hackerrank-solutions-no-prefix-set\/","title":{"rendered":"HackerRank Solutions: No Prefix Set"},"content":{"rendered":"\n<p class=\"has-text-align-center\"><strong>Problem<\/strong><\/p>\n\n\n\n<p>There is a given list of strings where each string contains only lowercase letters from&nbsp;, inclusive. The set of strings is said to be a&nbsp;<strong>GOOD SET<\/strong>&nbsp;if no string is a prefix of another string. In this case, print&nbsp;<strong>GOOD SET<\/strong>. Otherwise, print&nbsp;<strong>BAD SET<\/strong>&nbsp;on the first line followed by the string being checked.<\/p>\n\n\n\n<p><strong>Note<\/strong>&nbsp;If two strings are identical, they are prefixes of each other.<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"32\" src=\"https:\/\/sites.temple.edu\/vahid\/files\/2022\/03\/image.png\" alt=\"\" class=\"wp-image-160\" srcset=\"https:\/\/sites.temple.edu\/vahid\/files\/2022\/03\/image.png 367w, https:\/\/sites.temple.edu\/vahid\/files\/2022\/03\/image-300x26.png 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/figure>\n\n\n\n<p>Here &#8216;abcd&#8217; is a prefix of &#8216;abcde&#8217; and &#8216;bcd&#8217; is a prefix of &#8216;bcde&#8217;. Since &#8216;abcde&#8217; is tested first, print<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BAD SET  \nabcde\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"367\" height=\"32\" src=\"https:\/\/sites.temple.edu\/vahid\/files\/2022\/03\/image-1.png\" alt=\"\" class=\"wp-image-161\" srcset=\"https:\/\/sites.temple.edu\/vahid\/files\/2022\/03\/image-1.png 367w, https:\/\/sites.temple.edu\/vahid\/files\/2022\/03\/image-1-300x26.png 300w\" sizes=\"auto, (max-width: 367px) 100vw, 367px\" \/><\/figure>\n\n\n\n<p>No string is a prefix of another so print<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GOOD SET \n<\/code><\/pre>\n\n\n\n<p><strong>Function Description<\/strong><br>Complete the&nbsp;<em>noPrefix<\/em>&nbsp;function in the editor below.<\/p>\n\n\n\n<p><em>noPrefix<\/em>&nbsp;has the following parameter(s):<br>&#8211;&nbsp;<em>string words[n]:<\/em>&nbsp;an array of strings<\/p>\n\n\n\n<p><strong>Prints<\/strong><br>&#8211;&nbsp;<em>string(s):<\/em>&nbsp;either&nbsp;<strong>GOOD SET<\/strong>&nbsp;or&nbsp;<strong>BAD SET<\/strong>&nbsp;on one line followed by the word on the next line. No return value is expected.<\/p>\n\n\n\n<p><strong>Input Format<\/strong><br>First line contains&nbsp;, the size of&nbsp;.<br>Then next&nbsp;&nbsp;lines each contain a string,&nbsp;.<\/p>\n\n\n\n<p><strong>Constraints<\/strong><br><br>&nbsp;the length of words[i]&nbsp;<br>All letters in&nbsp;&nbsp;are in the range &#8216;a&#8217; through &#8216;j&#8217;, inclusive.<\/p>\n\n\n\n<p><strong>Sample Input00<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>STDIN       Function\n-----       --------\n7            words&#091;] size n = 7\naab          words = &#091;'aab', 'defgab', 'abcde', 'aabcde', 'bbbbbbbbbb', 'jabjjjad']\ndefgab  \nabcde\naabcde\ncedaaa\nbbbbbbbbbb\njabjjjad\n<\/code><\/pre>\n\n\n\n<p><strong>Sample Output00<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BAD SET\naabcde\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong><br>&#8216;aab&#8217; is prefix of &#8216;aabcde&#8217; so it is a&nbsp;<strong>BAD SET<\/strong>&nbsp;and fails at string &#8216;aabcde&#8217;.<\/p>\n\n\n\n<p><strong>Sample Input01<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>4\naab\naac\naacghgh\naabghgh\n<\/code><\/pre>\n\n\n\n<p><strong>Sample Output01<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>BAD SET\naacghgh\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong><br>&#8216;aab&#8217; is a prefix of &#8216;aabghgh&#8217;, and aac&#8217; is prefix of &#8216;aacghgh&#8217;. The set is a\u00a0<strong>BAD SET<\/strong>. &#8216;aacghgh&#8217; is tested before &#8216;aabghgh&#8217;, so and it fails at &#8216;aacghgh&#8217;.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Solution<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/python3\r\n\r\nimport math\r\nimport os\r\nimport random\r\nimport re\r\nimport sys\r\n\r\n#\r\n# Complete the 'noPrefix' function below.\r\n#\r\n# The function accepts STRING_ARRAY words as parameter.\r\n#\r\n\r\nclass Node:\r\n    def __init__(self, parent=None, childrean=dict()):\r\n        self.parent = parent\r\n        self.children = childrean\r\n        self.leaf = False\r\n\r\ndef noPrefix(words):\r\n    # Write your code here\r\n    # make Trie\r\n    root = Node(None)\r\n    for word in words:\r\n        node = root\r\n        prefix = False\r\n        for let in word:\r\n            exists = &#091;]\r\n            if let in node.children.keys():\r\n                child = node.children&#091;let]\r\n                exists.append(True)\r\n            else:\r\n                exists.append(False)\r\n                child = Node(node, dict())\r\n                node.children&#091;let] = child\r\n            node = child\r\n            if node.leaf:\r\n                prefix = True\r\n                break\r\n        if prefix | all(exists):\r\n            prefix = True\r\n            break\r\n        node.leaf = True\r\n    if prefix:\r\n        print('BAD SET')\r\n        print(word)\r\n    else:\r\n        print('GOOD SET')\r\n            \r\n    \r\n    \r\nif __name__ == '__main__':\r\n    n = int(input().strip())\r\n\r\n    words = &#091;]\r\n\r\n    for _ in range(n):\r\n        words_item = input()\r\n        words.append(words_item)\r\n\r\n    noPrefix(words)\r<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Problem There is a given list of strings where each string contains only lowercase letters from&nbsp;, inclusive. The set of strings is said to be&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/sites.temple.edu\/vahid\/2022\/03\/05\/hackerrank-solutions-no-prefix-set\/\">Continue reading<span class=\"screen-reader-text\">HackerRank Solutions: No Prefix Set<\/span><\/a><\/div>\n","protected":false},"author":18759,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[102,104,4,103],"class_list":["post-159","post","type-post","status-publish","format-standard","hentry","category-programming","tag-hackerrank","tag-programming","tag-python","tag-solution","entry"],"_links":{"self":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts\/159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/users\/18759"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/comments?post=159"}],"version-history":[{"count":0,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts\/159\/revisions"}],"wp:attachment":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/media?parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/categories?post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/tags?post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}