- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find orientation of a pattern in a matrix in C++
In this problem, we are given a matrix consisting of character values that make a pattern, we are also given a pattern to be found. Our task is to find the orientation (horizontal or vertical) of a pattern in a matrix.
Let’s take an example to understand the problem,
Input
mat[][] = { { r, a, m }, {a, m, c}, {w, f, t} } Patern : raw
Output
vertical
Solution Approach
A simple solution to the problem is by searching the M sized pattern in all the N rows of the matrix. This solution is ok but a more effective solution to the problem is by using the KML pattern matching algorithm for all rows and columns of the matrix.
Program to illustrate the working of our solution,
Example
#include<bits/stdc++.h> using namespace std; #define N 3 void calcLpsValues(char *pat, int M, int *lps) { int len = 0; int i = 1; lps[0] = 0; while (i < M) { if (pat[i] == pat[len]) { len++; lps[i++] = len; } else { if (len != 0) len = lps[len - 1]; else lps[i++] = 0; } } } int searchPattern(char *pat, char *txt) { int M = strlen(pat); int *lps = (int *)malloc(sizeof(int)*M); int j = 0; calcLpsValues(pat, M, lps); int i = 0; while (i < N) { if (pat[j] == txt[i]) { j++; i++; } if (j == M) return 1; else if (i < N && pat[j] != txt[i]) { if (j != 0) j = lps[j - 1]; else i = i + 1; } } return 0; } void findPatternOrientation(char mat[][N], char *pat) { char *col = (char*) malloc(N); for (int i = 0; i < N; i++) { if (searchPattern(pat, mat[i])) { cout<<"horizontal"; return; } for (int j = 0; j < N; j++) col[j] = *(mat[j] + i); if (searchPattern(pat, col)) cout<<"vertical"; } } int main() { char mat[N][N] = {{'r', 'a', 'm'}, {'a', 'm', 'c'}, {'w', 'f', 't'}}; char pattern[] = "raw"; cout<<"The orientation of the pattern in matrix is "; findPatternOrientation(mat, pattern); return 0; }
Output
The orientation of the pattern in matrix is vertical
- Related Articles
- Print concentric rectangular pattern in a 2d matrix in C++
- Print matrix in snake pattern in C Programming.
- Program to print a matrix in Diagonal Pattern.
- Find number of cavities in a matrix in C++
- Print numbers in matrix diagonal pattern in C Program.
- Printing string in plus ‘+’ pattern in the matrix in C++
- Print matrix in diagonal pattern
- Find the mean vector of a Matrix in C++
- Find safe cells in a matrix in C++
- Find a specific pair in Matrix in C++
- Find single Movement in a Matrix in C++
- Find maximum element of each column in a matrix in C++
- Find maximum element of each row in a matrix in C++
- Find Maximum side length of square in a Matrix in C++
- Find duplicate rows in a binary matrix in C++

Advertisements