Problem:-
You are given an n*m grid which contains lower case English letters. How many times does the phrase "saba" appear horizontally, vertically, and diagonally in the grid?
Input format
- First line: Two integer n and m, where n denotes (1 <= n,m <= 100) the number of rows and m denotes the number of columns in the grid
- Next n lines: Each line must contain a string of length m which contains lower-case English letters only
Output format
Print the number of times the word “saba” appears in the grid.
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
The phrase "saba" must look one of thease shapes :
s | |||
a | |||
b | |||
a |
s | a | b | a |
s | |||
a | |||
b | |||
a |
a | |||
b | |||
a | |||
s |
Code:-
This solution is based on the c++ language and you can submit ib c++14 and c++17 also.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
char s[n+1][m+1];
for(int i=0;i<n;i++)
cin>>s[i];
int ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(s[i][j]=='s')
{
// for checking in horizontal direction
if(j+3<m)
{
if(s[i][j]=='s' && s[i][j+1]=='a' && s[i][j+2]=='b' && s[i][j+3]=='a')
ans++;
// for checking in downward direction
if(i+3<n)
{
if(s[i][j]=='s' && s[i+1][j+1]=='a' && s[i+2][j+2]=='b' && s[i+3][j+3]=='a')
ans++;
}
}
// for checking in vertical direction
if(i+3<n)
{
if(s[i][j]=='s' && s[i+1][j]=='a' && s[i+2][j]=='b' && s[i+3][j]=='a')
ans++;
}
// for checking in upward diagonal
if((i-3)>=0 && (j+3)<m)
{
if(s[i][j]=='s' && s[i-1][j+1]=='a' && s[i-2][j+2]=='b' && s[i-3][j+3]=='a')
ans++;
}
}
}
}
// printing of ans
cout<<ans;
return 0;
}
Recommended Post:-
- Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-Data structure:-- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
0 Comments