Open In App

Check if the Matrix satisfies the given conditions

Last Updated : 07 Oct, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a matrix mat[][], the task is to check whether the matrix is valid or not. A valid matrix is the matrix that satisfies the given conditions: 

  1. For every row, it must contain a single distinct character.
  2. No two consecutive rows have a character in common.

Examples: 

Input: mat[][] = { 
{0, 0, 0}, 
{1, 1, 1}, 
{0, 0, 2}} 
Output: No 
The last row doesn't consist of a single distinct character.

Input: mat[][] = { 
{8, 8, 8, 8, 8}, 
{4, 4, 4, 4, 4}, 
{6, 6, 6, 6, 6}, 
{5, 5, 5, 5, 5}, 
{8, 8, 8, 8, 8}} 
Output: Yes 

Approach: First of all check the first row if it contains same characters or not. If it contains same characters then iterate the second row and compare the characters of the current row with the first element of the previous row, if the two elements are equal or the characters of the current row are different, return false. Repeat the above process for all the consecutive row.

Below is the implementation of the above approach:  

C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;

// Storing the size of the matrix
const int M = 5, N = 5;

// Function that returns true if
// the matrix is valid
bool isPerfect(char board[M][N + 1])
{
    char m;

    for (int i = 0; i < M; i++) {

        // Get the current row
        string s = board[i];

        // Comparing first element
        // of the row with the element
        // of previous row
        if (i > 0 && s[0] == m)
            return false;

        // Checking if all the characters of the
        // current row are same or not
        for (int j = 0; j < N; j++) {

            // Storing the first character
            if (j == 0)
                m = s[0];

            // Comparing all the elements
            // with the first element
            else {
                if (m != s[j])
                    return false;
            }
        }
    }

    return true;
}

// Driver code
int main()
{
    char board[M][N + 1] = { "88888",
                             "44444",
                             "66666",
                             "55555",
                             "88888" };

    if (isPerfect(board))
        cout << "Yes";
    else
        cout << "No";

    return 0;
}
Java
// Java implementation of the approach 
class GFG 
{
    
    // Storing the size of the matrix 
    static final int M = 5, N = 5; 
    
    // Function that returns true if 
    // the matrix is valid 
    static boolean isPerfect(String board[]) 
    { 
        char m = 0;
    
        for (int i = 0; i < M; i++)
        { 
    
            // Get the current row 
            String s = board[i]; 
    
            // Comparing first element 
            // of the row with the element 
            // of previous row 
            if (i > 0 && s.charAt(0) == m) 
                return false; 
    
            // Checking if all the characters of the 
            // current row are same or not 
            for (int j = 0; j < N; j++) 
            { 
    
                // Storing the first character 
                if (j == 0) 
                    m = s.charAt(0); 
    
                // Comparing all the elements 
                // with the first element 
                else 
                { 
                    if (m != s.charAt(j)) 
                        return false; 
                } 
            } 
        } 
        return true; 
    } 
    
    // Driver code 
    public static void main (String[] args) 
    { 
        String board[] = { "88888", "44444", 
                           "66666", "55555", 
                           "88888" }; 
    
        if (isPerfect(board)) 
            System.out.println("Yes"); 
        else
            System.out.println("No"); 
    } 
}
    
// This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach

# Storing the size of the matrix
M = 5
N = 5

# Function that returns true if
# the matrix is valid
def isPerfect(board):
    m = ''

    for i in range(M):

        # Get the current row
        s = board[i]

        # Comparing first element
        # of the row with the element
        # of previous row
        if (i > 0 and s[0] == m):
            return False

        # Checking if all the characters of the
        # current row are same or not
        for j in range(N):

            # Storing the first character
            if (j == 0):
                m = s[0]

            # Comparing all the elements
            # with the first element
            else:
                if (m != s[j]):
                    return False

    return True

# Driver code
board = ["88888",
         "44444",
         "66666",
         "55555",
         "88888"]

if (isPerfect(board)):
    print("Yes")
else:
    print("No")

# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
    
class GFG 
{
    
    // Storing the size of the matrix 
    static readonly int M = 5, N = 5; 
    
    // Function that returns true if 
    // the matrix is valid 
    static bool isPerfect(String []board) 
    { 
        char m = '0';
    
        for (int i = 0; i < M; i++)
        { 
    
            // Get the current row 
            String s = board[i]; 
    
            // Comparing first element 
            // of the row with the element 
            // of previous row 
            if (i > 0 && s[0] == m) 
                return false; 
    
            // Checking if all the characters of the 
            // current row are same or not 
            for (int j = 0; j < N; j++) 
            { 
    
                // Storing the first character 
                if (j == 0) 
                    m = s[0]; 
    
                // Comparing all the elements 
                // with the first element 
                else
                { 
                    if (m != s[j]) 
                        return false; 
                } 
            } 
        } 
        return true; 
    } 
    
    // Driver code 
    public static void Main (String[] args) 
    { 
        String []board = { "88888", "44444", 
                           "66666", "55555", 
                           "88888" }; 
    
        if (isPerfect(board)) 
            Console.WriteLine("Yes"); 
        else
            Console.WriteLine("No"); 
    } 
}

// This code is contributed by Rajput-Ji
JavaScript
<script>

// Javascript implementation of the approach

// Storing the size of the matrix
var M = 5, N = 5;

// Function that returns true if
// the matrix is valid
function isPerfect(board)
{
    var m;

    for(var i = 0; i < M; i++) 
    {
        
        // Get the current row
        var s = board[i];

        // Comparing first element
        // of the row with the element
        // of previous row
        if (i > 0 && s[0] == m)
            return false;

        // Checking if all the characters of the
        // current row are same or not
        for(var j = 0; j < N; j++)
        {
            
            // Storing the first character
            if (j == 0)
                m = s[0];

            // Comparing all the elements
            // with the first element
            else
            {
                if (m != s[j])
                    return false;
            }
        }
    }
    return true;
}

// Driver code
var board = [ "88888", "44444",
              "66666", "55555", 
              "88888" ];
              
if (isPerfect(board))
    document.write("Yes");
else
    document.write("No");
    
// This code is contributed by itsok

</script>

Output: 
Yes

 

Time complexity: O(M*N)
Auxiliary space: O(N) because using extra space for string s


Next Article

Similar Reads

  翻译: