`{"id":"a4c12a56-ec86-4926-b476-6ae6ee3e4f87","name":"Find And Replace Pattern","description":"1. Given a list of strings words and a string pattern.\r\n2. Return a list of words[i] that match pattern. You may return the answer in any order.\r\n3. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.\r\n4. Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.\r\n","inputFormat":"Input: words = [\"mno\",\"rst\",\"rss\",\"bll\",\"lml\",\"aaa\"], pattern = \"epp\"\r\n\r\nInput : Words = [\"p\", \"e\", \"c\"], pattern = \"a\"\r\n\r\nInput : Words = [\"abc\", \"pqr\", \"tuv\"], pattern = \"aaa\"\r\n","outputFormat":"Output: [\"rss\",\"bll\"]\r\nExplanation: \"rss\" matches the pattern because there is a permutation {e -> r, p -> s, ...}. \r\n\"aaa\" does not match the pattern because {e -> a, p -> a, ...} is not a permutation, since e and p map to the same letter.\r\n\r\nOutput : [\"p\", \"e\", \"c\"]\r\n\r\nOutput : [] -> Empty","constraints":"1. 1 &lt;= pattern.length &lt;= 20\r\n2. 1 &lt;= words.length &lt;= 20\r\n3. words[i].length == pattern.length\r\n4. pattern and words[i] are lowercase English letters.\r\n","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nvector<string> findAndReplacePattern(vector<string> words, string pattern) {\n // write your code here\n}\n\nint main() {\n\n string pattern;\n getline(cin,pattern);\n int n = 0;\n cin>>n;\n // to consume enter after number\n vector<string> words(n);\n for (int i = 0; i < n; i++) {\n getline(cin,words[i]);\n }\n vector<string> res = findAndReplacePattern(words, pattern);\n if (res.size() == 0) {\n cout<<\"Empty\";\n return 0;\n }\n sort(res.begin(),res.end());\n for (int i=0;i<res.size();i++) {\n cout<<res[i]<<\" \";\n }\n\n return 0;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~User''s Section~~~~~~~~~~~~~~\r\n public static List<String> findAndReplacePattern(String[] words, String pattern) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n String pattern = scn.nextLine();\r\n int n = scn.nextInt();\r\n scn.nextLine(); // to consume enter after number\r\n String[] words = new String[n];\r\n for (int i = 0; i < n; i++) {\r\n words[i] = scn.nextLine();\r\n }\r\n List<String> res = findAndReplacePattern(words, pattern);\r\n if (res.size() == 0) {\r\n System.out.println(\"Empty\");\r\n return;\r\n }\r\n Collections.sort(res);\r\n for (String str : res) {\r\n System.out.print(str + \" \");\r\n }\r\n System.out.println();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"epp\r\n6\r\nmno\r\nrst\r\nrss\r\nbll\r\nlml\r\naaa","sampleOutput":"bll rss ","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":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"52decccd-04a7-4e5f-a1fb-d99a0f3a1937","name":"Find And Replace Pattern","slug":"find-and-replace-pattern","type":1}],"next":{"id":"e5d8cf26-6355-4cac-9b21-3ed1d5f5abb0","name":"Find And Replace Pattern MCQ","type":0,"slug":"find-and-replace-pattern-mcq"},"prev":{"id":"b25e974b-8e18-4573-9ab0-e671d28da727","name":"Battleship In Board MCQ","type":0,"slug":"battleship-in-board-mcq"}}}`

# Find And Replace Pattern

1. Given a list of strings words and a string pattern. 2. Return a list of words[i] that match pattern. You may return the answer in any order. 3. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word. 4. Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.

`{"id":"a4c12a56-ec86-4926-b476-6ae6ee3e4f87","name":"Find And Replace Pattern","description":"1. Given a list of strings words and a string pattern.\r\n2. Return a list of words[i] that match pattern. You may return the answer in any order.\r\n3. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word.\r\n4. Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.\r\n","inputFormat":"Input: words = [\"mno\",\"rst\",\"rss\",\"bll\",\"lml\",\"aaa\"], pattern = \"epp\"\r\n\r\nInput : Words = [\"p\", \"e\", \"c\"], pattern = \"a\"\r\n\r\nInput : Words = [\"abc\", \"pqr\", \"tuv\"], pattern = \"aaa\"\r\n","outputFormat":"Output: [\"rss\",\"bll\"]\r\nExplanation: \"rss\" matches the pattern because there is a permutation {e -> r, p -> s, ...}. \r\n\"aaa\" does not match the pattern because {e -> a, p -> a, ...} is not a permutation, since e and p map to the same letter.\r\n\r\nOutput : [\"p\", \"e\", \"c\"]\r\n\r\nOutput : [] -> Empty","constraints":"1. 1 &lt;= pattern.length &lt;= 20\r\n2. 1 &lt;= words.length &lt;= 20\r\n3. words[i].length == pattern.length\r\n4. pattern and words[i] are lowercase English letters.\r\n","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nvector<string> findAndReplacePattern(vector<string> words, string pattern) {\n // write your code here\n}\n\nint main() {\n\n string pattern;\n getline(cin,pattern);\n int n = 0;\n cin>>n;\n // to consume enter after number\n vector<string> words(n);\n for (int i = 0; i < n; i++) {\n getline(cin,words[i]);\n }\n vector<string> res = findAndReplacePattern(words, pattern);\n if (res.size() == 0) {\n cout<<\"Empty\";\n return 0;\n }\n sort(res.begin(),res.end());\n for (int i=0;i<res.size();i++) {\n cout<<res[i]<<\" \";\n }\n\n return 0;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~~~~~~User''s Section~~~~~~~~~~~~~~\r\n public static List<String> findAndReplacePattern(String[] words, String pattern) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~~~~~~Input Management~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n String pattern = scn.nextLine();\r\n int n = scn.nextInt();\r\n scn.nextLine(); // to consume enter after number\r\n String[] words = new String[n];\r\n for (int i = 0; i < n; i++) {\r\n words[i] = scn.nextLine();\r\n }\r\n List<String> res = findAndReplacePattern(words, pattern);\r\n if (res.size() == 0) {\r\n System.out.println(\"Empty\");\r\n return;\r\n }\r\n Collections.sort(res);\r\n for (String str : res) {\r\n System.out.print(str + \" \");\r\n }\r\n System.out.println();\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"epp\r\n6\r\nmno\r\nrst\r\nrss\r\nbll\r\nlml\r\naaa","sampleOutput":"bll rss ","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":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"52decccd-04a7-4e5f-a1fb-d99a0f3a1937","name":"Find And Replace Pattern","slug":"find-and-replace-pattern","type":1}],"next":{"id":"e5d8cf26-6355-4cac-9b21-3ed1d5f5abb0","name":"Find And Replace Pattern MCQ","type":0,"slug":"find-and-replace-pattern-mcq"},"prev":{"id":"b25e974b-8e18-4573-9ab0-e671d28da727","name":"Battleship In Board MCQ","type":0,"slug":"battleship-in-board-mcq"}}}` Editor

# Find And Replace Pattern

medium

1. Given a list of strings words and a string pattern. 2. Return a list of words[i] that match pattern. You may return the answer in any order. 3. A word matches the pattern if there exists a permutation of letters p so that after replacing every letter x in the pattern with p(x), we get the desired word. 4. Recall that a permutation of letters is a bijection from letters to letters: every letter maps to another letter, and no two letters map to the same letter.

## Constraints

1. 1 <= pattern.length <= 20 2. 1 <= words.length <= 20 3. words[i].length == pattern.length 4. pattern and words[i] are lowercase English letters.

## Format

### Input

Input: words = ["mno","rst","rss","bll","lml","aaa"], pattern = "epp" Input : Words = ["p", "e", "c"], pattern = "a" Input : Words = ["abc", "pqr", "tuv"], pattern = "aaa"

### Output

Output: ["rss","bll"] Explanation: "rss" matches the pattern because there is a permutation {e -> r, p -> s, ...}. "aaa" does not match the pattern because {e -> a, p -> a, ...} is not a permutation, since e and p map to the same letter. Output : ["p", "e", "c"] Output : [] -> Empty

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}epp 6 mno rst rss bll lml aaa```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}bll rss `

Discussions

Show Discussion

Related Resources 