`{"id":"33b18df1-2d63-489e-93b7-223c64eebfc3","name":"Number Of Valid Words","description":"1. You are given N number of words.\r\n2. You are given M puzzles in the form of M strings.\r\n3. For a given puzzle, a word is valid if both the following conditions are confirmed - \r\n Condition 1 -> Word contains the first letter of puzzle.\r\n Condition 2 -> For each letter in word, that letter should be present in puzzle.\r\n4. You have to print the number of valid words corresponding to a puzzle.","inputFormat":"A number N\r\nN space separated strings\r\nA number M\r\nM space separated strings\r\n","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= N &lt;= 10^5\r\n4 &lt;= length of word &lt;= 50\r\n1 &lt;= M &lt;= 10^4\r\nlength of puzzle string = 7\r\npuzzle string doesn't contain any repeated characters.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvector<int> findNumOfValidWords(vector<string> &words,vector<string> &puzzles)\n{\n \n}\n\nint main()\n{\n int n;cin>>n;\n vector<string> words(n);\n for(int i=0;i<n;++i) cin>>words[i];\n int m;cin>>m;\n vector<string> puzzles(m);\n for(int i=0;i<m;++i) cin>>puzzles[i];\n vector<int> ans=findNumOfValidWords(words,puzzles);\n for(int i=0;i<m;++i) \n cout<<puzzles[i]<<\" -> \"<<ans[i]<<\"\\n\";\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static ArrayList<Integer> findNumOfValidWords(String[] words, String[] puzzles) {\r\n\t\t//write your code here\r\n\t\t\r\n\t\treturn null;\r\n\t }\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint n = scn.nextInt();\r\n\t\tString[] words = new String[n];\r\n\t\tfor(int i = 0 ; i < words.length; i++) {\r\n\t\t\twords[i] = scn.next();\r\n\t\t}\r\n\t\tint m = scn.nextInt();\r\n\t\tString[] puzzles = new String[m];\r\n\t\tfor(int i = 0 ; i < m ;i++) {\r\n\t\t\tpuzzles[i] = scn.next();\r\n\t\t}\r\n\t\tArrayList<Integer> ans = findNumOfValidWords(words,puzzles);\r\n\t\tfor(int val : ans) {\r\n\t\t\tSystem.out.println(puzzles[i] + \" -> \" + ans.get(i));\r\n\t\t}\r\n\t}\r\n\t\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"7\r\naaaa asas able ability actt actor access \r\n6\r\naboveyz abrodyz abslute absoryz actresz gaswxyz\r\n","sampleOutput":"aboveyz -> 1\r\nabrodyz -> 1\r\nabslute -> 3\r\nabsoryz -> 2\r\nactresz -> 4\r\ngaswxyz -> 0","questionVideo":"https://www.youtube.com/embed/9jV0CC_C26s?end=331","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":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"30f079dc-7677-46ab-b6c8-83e02885f9b9","name":"Number Of Valid Words","slug":"number-of-valid-words","type":1}],"next":{"id":"7cffb3d8-c1c5-42ec-80f9-628c5212c4a9","name":"Number of Valid Words MCQ","type":0,"slug":"number-of-valid-words-mcq"},"prev":{"id":"bed30c10-4e23-4f30-9e5b-b7cee2568534","name":"Minimum Number of Software Developers","type":3,"slug":"minimum-number-of-software-developers"}}}`

# Number Of Valid Words

1. You are given N number of words. 2. You are given M puzzles in the form of M strings. 3. For a given puzzle, a word is valid if both the following conditions are confirmed - Condition 1 -> Word contains the first letter of puzzle. Condition 2 -> For each letter in word, that letter should be present in puzzle. 4. You have to print the number of valid words corresponding to a puzzle.

`{"id":"33b18df1-2d63-489e-93b7-223c64eebfc3","name":"Number Of Valid Words","description":"1. You are given N number of words.\r\n2. You are given M puzzles in the form of M strings.\r\n3. For a given puzzle, a word is valid if both the following conditions are confirmed - \r\n Condition 1 -> Word contains the first letter of puzzle.\r\n Condition 2 -> For each letter in word, that letter should be present in puzzle.\r\n4. You have to print the number of valid words corresponding to a puzzle.","inputFormat":"A number N\r\nN space separated strings\r\nA number M\r\nM space separated strings\r\n","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= N &lt;= 10^5\r\n4 &lt;= length of word &lt;= 50\r\n1 &lt;= M &lt;= 10^4\r\nlength of puzzle string = 7\r\npuzzle string doesn't contain any repeated characters.","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n\nvector<int> findNumOfValidWords(vector<string> &words,vector<string> &puzzles)\n{\n \n}\n\nint main()\n{\n int n;cin>>n;\n vector<string> words(n);\n for(int i=0;i<n;++i) cin>>words[i];\n int m;cin>>m;\n vector<string> puzzles(m);\n for(int i=0;i<m;++i) cin>>puzzles[i];\n vector<int> ans=findNumOfValidWords(words,puzzles);\n for(int i=0;i<m;++i) \n cout<<puzzles[i]<<\" -> \"<<ans[i]<<\"\\n\";\n return 0;\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static ArrayList<Integer> findNumOfValidWords(String[] words, String[] puzzles) {\r\n\t\t//write your code here\r\n\t\t\r\n\t\treturn null;\r\n\t }\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint n = scn.nextInt();\r\n\t\tString[] words = new String[n];\r\n\t\tfor(int i = 0 ; i < words.length; i++) {\r\n\t\t\twords[i] = scn.next();\r\n\t\t}\r\n\t\tint m = scn.nextInt();\r\n\t\tString[] puzzles = new String[m];\r\n\t\tfor(int i = 0 ; i < m ;i++) {\r\n\t\t\tpuzzles[i] = scn.next();\r\n\t\t}\r\n\t\tArrayList<Integer> ans = findNumOfValidWords(words,puzzles);\r\n\t\tfor(int val : ans) {\r\n\t\t\tSystem.out.println(puzzles[i] + \" -> \" + ans.get(i));\r\n\t\t}\r\n\t}\r\n\t\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"7\r\naaaa asas able ability actt actor access \r\n6\r\naboveyz abrodyz abslute absoryz actresz gaswxyz\r\n","sampleOutput":"aboveyz -> 1\r\nabrodyz -> 1\r\nabslute -> 3\r\nabsoryz -> 2\r\nactresz -> 4\r\ngaswxyz -> 0","questionVideo":"https://www.youtube.com/embed/9jV0CC_C26s?end=331","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":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"30f079dc-7677-46ab-b6c8-83e02885f9b9","name":"Number Of Valid Words","slug":"number-of-valid-words","type":1}],"next":{"id":"7cffb3d8-c1c5-42ec-80f9-628c5212c4a9","name":"Number of Valid Words MCQ","type":0,"slug":"number-of-valid-words-mcq"},"prev":{"id":"bed30c10-4e23-4f30-9e5b-b7cee2568534","name":"Minimum Number of Software Developers","type":3,"slug":"minimum-number-of-software-developers"}}}`

Editor

# Number Of Valid Words

hard

1. You are given N number of words. 2. You are given M puzzles in the form of M strings. 3. For a given puzzle, a word is valid if both the following conditions are confirmed - Condition 1 -> Word contains the first letter of puzzle. Condition 2 -> For each letter in word, that letter should be present in puzzle. 4. You have to print the number of valid words corresponding to a puzzle.

## Constraints

1 <= N <= 10^5 4 <= length of word <= 50 1 <= M <= 10^4 length of puzzle string = 7 puzzle string doesn't contain any repeated characters.

## Format

### Input

A number N N space separated strings A number M M space separated strings

### Output

Check the sample ouput and question video.

## 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;}7 aaaa asas able ability actt actor access 6 aboveyz abrodyz abslute absoryz actresz gaswxyz ```

### 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;}aboveyz -> 1 abrodyz -> 1 abslute -> 3 absoryz -> 2 actresz -> 4 gaswxyz -> 0```

Question Video

Discussions

Show Discussion

Related Resources