{"id":"be322da1-ebcd-4666-a89b-edb9a379b30d","name":"Longest Word In Dictionary","description":"1. Given an array of strings words.\r\n2. Find the word of longest length, where all of its substrings starting from 0th index exists as different word in array.\r\n3. If there are multiple answers with same length return one which is smallest in lexicographical order. If there is no valid string return empty string.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= words.length &lt;= 1000\r\n2. 1 &lt;= words[i].length &lt;= 30\r\n3. words[i] consists of lowercase English letters.","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static String longestWord(String[] words) {\r\n\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n\r\n String[]words = new String[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n words[i] = read.readLine();\r\n }\r\n\r\n String result = longestWord(words);\r\n System.out.println(result);\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"7\r\na\r\nbanana\r\napp\r\nappl\r\nap\r\napply\r\napple","sampleOutput":"apple\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"4b4725b5-4896-4b68-aae2-03ea1e2ecbb2","name":"Trie For Intermediate","slug":"trie-for-intermediate-9996","type":0},{"id":"c2079e60-a16a-40bd-8f5e-8d66904d148a","name":"Longest Word In Dictionary MCQ","slug":"longest-word-in-dictionary-mcq","type":0},{"id":"43ca4b73-ef5f-4857-b1cc-52bb516ff312","name":"Longest Word In Dictionary","slug":"longest-word-in-dictionary","type":1}],"next":{"id":"1d449d94-30d0-44a9-9d88-765d5a80af32","name":"Longest Word In Dictionary Through Deleting","type":1,"slug":"longest-word-in-dictionary-through-deleting"},"prev":null}}

Longest Word In Dictionary

1. Given an array of strings words. 2. Find the word of longest length, where all of its substrings starting from 0th index exists as different word in array. 3. If there are multiple answers with same length return one which is smallest in lexicographical order. If there is no valid string return empty string.

{"id":"be322da1-ebcd-4666-a89b-edb9a379b30d","name":"Longest Word In Dictionary","description":"1. Given an array of strings words.\r\n2. Find the word of longest length, where all of its substrings starting from 0th index exists as different word in array.\r\n3. If there are multiple answers with same length return one which is smallest in lexicographical order. If there is no valid string return empty string.","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= words.length &lt;= 1000\r\n2. 1 &lt;= words[i].length &lt;= 30\r\n3. words[i] consists of lowercase English letters.","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static String longestWord(String[] words) {\r\n\r\n }\r\n\r\n public static void main(String[] args) throws Exception {\r\n BufferedReader read = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int n = Integer.parseInt(read.readLine());\r\n\r\n String[]words = new String[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n words[i] = read.readLine();\r\n }\r\n\r\n String result = longestWord(words);\r\n System.out.println(result);\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"7\r\na\r\nbanana\r\napp\r\nappl\r\nap\r\napply\r\napple","sampleOutput":"apple\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"4b4725b5-4896-4b68-aae2-03ea1e2ecbb2","name":"Trie For Intermediate","slug":"trie-for-intermediate-9996","type":0},{"id":"c2079e60-a16a-40bd-8f5e-8d66904d148a","name":"Longest Word In Dictionary MCQ","slug":"longest-word-in-dictionary-mcq","type":0},{"id":"43ca4b73-ef5f-4857-b1cc-52bb516ff312","name":"Longest Word In Dictionary","slug":"longest-word-in-dictionary","type":1}],"next":{"id":"1d449d94-30d0-44a9-9d88-765d5a80af32","name":"Longest Word In Dictionary Through Deleting","type":1,"slug":"longest-word-in-dictionary-through-deleting"},"prev":null}}
plane

Editor


Loading...

Longest Word In Dictionary

medium

1. Given an array of strings words. 2. Find the word of longest length, where all of its substrings starting from 0th index exists as different word in array. 3. If there are multiple answers with same length return one which is smallest in lexicographical order. If there is no valid string return empty string.

Constraints

1. 1 <= words.length <= 1000 2. 1 <= words[i].length <= 30 3. words[i] consists of lowercase English letters.

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

7 a banana app appl ap apply apple

Sample Output

apple

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode