Random keyboard

3.7

3 votes
String Algorithms, Real world, Algorithms, String Searching
Problem

During your computer examination, you are given a question to find whether a given word is valid if it can be typed using only one row of the given keyboard pattern. 

You are given an integer N where N denotes the number of rows in keyboards. You are given an array of string keyboard where keyboard denotes the key sequence of each row i.e. keyboard[i] denotes the key sequence of ith row. You are given a string word that you need to validate.

Task

Return 1 if the string word is valid else 0.

Example

Assumptions

  • N = 3
  • keyboard = [asdfghjkl, qwertyuiop, zxcvbnm]
  • word = akash

Approach

All the letters of string once are present in the first row of the keyboard.

Therefore return 1.

Function description

Complete the Solve() function provided in the editor below that takes the following arguments and returns 1 if the string word is valid else return 0:

  • N: Represents the total number of rows on the keyboard.
  • keyboard: Represents the key sequence of each row of the keyboard.
  • word: Represents the string that needs to be validated.

Input format

  • The first line contains an integer N denoting the total number of rows in the keyboard
  • Next, N line contains the key sequence of the Keyboard.
  • The last line contains a string that needs to be validated.

Output format

Print 1 if the string word is valid else 0.

Constraints

1N26

keyboard[i] contains lowercase English alphabets and can contain at max 26 letters without repetition.
words[i] contains lowercase English alphabets and can contain at max 10letters.

Sample Input
4
srdy
oncwejxbtkahu
ifmqp
lvgz
once
Sample Output
1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

All the letters of string once are present in the second row of the keyboard. Therefore return 1.

Editor Image

?