{"id":"22c95f05-1ac1-43ef-8583-8a55aadde707","name":"Replace Words","description":"1. You r given an array of string's say dictionary and a long string say sentence.\r\n2. You have to replace all words in sentence which contains a prefix in dictionary with the prefix itself in dictionary.\r\n\r\nExample \r\ndictionary = [\"cat\",\"bat\",\"rat\"]\r\nsentence = \"the cattle was rattled by the battery\"\r\n\r\nIn this sentence cattle, rattled and battery are the words containing prefix in dictionary they are cat, rat and bat.\r\nsentence after replace words is \"the cat was rat by the bat\".\r\n\r\n3. If there are multiple prefix for a word in dictionary replace it with the shortest prefix word.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= dictionary.length &lt;= 1000\r\n2. 1 &lt;= dictionary[i].length &lt;= 100\r\n3. 1 &lt;= sentence.length &lt;= 10^6\r\n4. dictionary[i] an sentence consists of only lower-case letters.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nstring replaceWords(vector<string>& dictionary, string sentence)\n{\nreturn \"\"; \n}\n\n\nint main()\n{\n int n;\n cin>>n;\n // cout << n;\n vector<string> dictionary(n);\n for(int i=0;i<n;i++)\n {\n cin >> dictionary[i];\n // cout << dictionary[i] << \" \";\n }\n \n string sentence;\n getline(cin,sentence);\n getline(cin,sentence);\n // cout << sentence << endl;\n string result = replaceWords(dictionary,sentence);\n cout << result;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static String replaceWords(String dictionary[], String sentence) {\r\n return \"\";\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[]dictionary = new String[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n dictionary[i] = read.readLine();\r\n }\r\n\r\n String sentence = read.readLine();\r\n\r\n String result = replaceWords(dictionary, sentence);\r\n System.out.println(result);\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\ncatt\r\ncat\r\nbat\r\nrat\r\nthe cattle was rattled by the battery\r\n","sampleOutput":"the cat was rat by the bat\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":"599f4af4-4011-40d7-988b-33bab754c561","name":"Replace Words MCQ","slug":"replace-words-mcq","type":0},{"id":"11025000-62a9-435e-b8b4-dce8c6f25f0e","name":"Replace Words","slug":"replace-words","type":1}],"next":null,"prev":null}}

Replace Words

1. You r given an array of string's say dictionary and a long string say sentence. 2. You have to replace all words in sentence which contains a prefix in dictionary with the prefix itself in dictionary. Example dictionary = ["cat","bat","rat"] sentence = "the cattle was rattled by the battery" In this sentence cattle, rattled and battery are the words containing prefix in dictionary they are cat, rat and bat. sentence after replace words is "the cat was rat by the bat". 3. If there are multiple prefix for a word in dictionary replace it with the shortest prefix word.

{"id":"22c95f05-1ac1-43ef-8583-8a55aadde707","name":"Replace Words","description":"1. You r given an array of string's say dictionary and a long string say sentence.\r\n2. You have to replace all words in sentence which contains a prefix in dictionary with the prefix itself in dictionary.\r\n\r\nExample \r\ndictionary = [\"cat\",\"bat\",\"rat\"]\r\nsentence = \"the cattle was rattled by the battery\"\r\n\r\nIn this sentence cattle, rattled and battery are the words containing prefix in dictionary they are cat, rat and bat.\r\nsentence after replace words is \"the cat was rat by the bat\".\r\n\r\n3. If there are multiple prefix for a word in dictionary replace it with the shortest prefix word.\r\n","inputFormat":"Input is managed for you","outputFormat":"Output is managed for you","constraints":"1. 1 &lt;= dictionary.length &lt;= 1000\r\n2. 1 &lt;= dictionary[i].length &lt;= 100\r\n3. 1 &lt;= sentence.length &lt;= 10^6\r\n4. dictionary[i] an sentence consists of only lower-case letters.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nstring replaceWords(vector<string>& dictionary, string sentence)\n{\nreturn \"\"; \n}\n\n\nint main()\n{\n int n;\n cin>>n;\n // cout << n;\n vector<string> dictionary(n);\n for(int i=0;i<n;i++)\n {\n cin >> dictionary[i];\n // cout << dictionary[i] << \" \";\n }\n \n string sentence;\n getline(cin,sentence);\n getline(cin,sentence);\n // cout << sentence << endl;\n string result = replaceWords(dictionary,sentence);\n cout << result;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n public static String replaceWords(String dictionary[], String sentence) {\r\n return \"\";\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[]dictionary = new String[n];\r\n\r\n for (int i = 0; i < n; i++) {\r\n dictionary[i] = read.readLine();\r\n }\r\n\r\n String sentence = read.readLine();\r\n\r\n String result = replaceWords(dictionary, sentence);\r\n System.out.println(result);\r\n\r\n }\r\n}\r\n"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\ncatt\r\ncat\r\nbat\r\nrat\r\nthe cattle was rattled by the battery\r\n","sampleOutput":"the cat was rat by the bat\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":"599f4af4-4011-40d7-988b-33bab754c561","name":"Replace Words MCQ","slug":"replace-words-mcq","type":0},{"id":"11025000-62a9-435e-b8b4-dce8c6f25f0e","name":"Replace Words","slug":"replace-words","type":1}],"next":null,"prev":null}}
plane

Editor


Loading...

Replace Words

easy

1. You r given an array of string's say dictionary and a long string say sentence. 2. You have to replace all words in sentence which contains a prefix in dictionary with the prefix itself in dictionary. Example dictionary = ["cat","bat","rat"] sentence = "the cattle was rattled by the battery" In this sentence cattle, rattled and battery are the words containing prefix in dictionary they are cat, rat and bat. sentence after replace words is "the cat was rat by the bat". 3. If there are multiple prefix for a word in dictionary replace it with the shortest prefix word.

Constraints

1. 1 <= dictionary.length <= 1000 2. 1 <= dictionary[i].length <= 100 3. 1 <= sentence.length <= 10^6 4. dictionary[i] an sentence consists of only lower-case letters.

Format

Input

Input is managed for you

Output

Output is managed for you

Example

Sample Input

4 catt cat bat rat the cattle was rattled by the battery

Sample Output

the cat was rat by the bat

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode