给定大小为M * N的二进制矩阵mat [] [] 。任务是检查从矩阵中删除一列后,矩阵的行是否唯一。
例子:
Input: mat[][] = {
{1 0 1},
{0 0 0},
{1 0 0}
}
Output: Yes
After deleting 2nd column each row of matrix become unique.
Input:mat[][] = {
{1 0},
{1 0}
}
Output: No
方法:
- 将矩阵作为字符串数组,并将每一行视为单个字符串。
- 初始化变量count = 0以对矩阵中的重复行进行计数。
- 现在从i = 0到str.length()的两个嵌套循环外循环,从j = 0到i的内循环,对每个索引检查str [i] == str [j]然后将count递增1并中断环形。
- 现在检查count> = 1,然后打印“否”,然后打印“是” 。
下面是上述方法的实现:
C++
// C++ program to check whether the
// Rows of Binary Matrix become unique
// After Deleting a column.
#include
using namespace std;
// Function to check whether rows of
// Binary matrix become unique after
// Deleting a column of from matrix.
int uniqueRows(string s[], int m, int n)
{
// Initialize variable count that
// stores the count of duplicate rows.
int i, j, count = 0;
// Take two nested loop and
// check weather rows already
// get seen then increment
// count by 1 then break the loop.
for (i = 0; i < n; i++) {
for (j = 0; j < i; j++) {
if (s[i] == s[j]) {
count++;
break;
}
}
}
// Check if count>=1 then print No
// Else print Yes.
if (count >= 1) {
cout << "No" << endl;
}
else {
cout << "Yes" << endl;
}
return 0;
}
// Driver code.
int main()
{
int m = 3, n = 3;
string s[] = {
{ 1, 0, 1 },
{ 0, 0, 0 },
{ 1, 0, 0 }
};
// Function calling
uniqueRows(s, m, n);
return 0;
}
Java
// Java program to check whether the
// Rows of Binary Matrix become unique
// After Deleting a column.
class GFG
{
// Function to check whether rows of
// Binary matrix become unique after
// Deleting a column of from matrix.
static int uniqueRows(int [][]s,
int m, int n)
{
// Initialize variable count that
// stores the count of duplicate rows.
int i, j, count = 0;
// Take two nested loop and
// check weather rows already
// get seen then increment
// count by 1 then break the loop.
for (i = 0; i < n; i++)
{
for (j = 0; j < i; j++)
{
if (s[i] == s[j])
{
count++;
break;
}
}
}
// Check if count>=1 then print No
// Else print Yes.
if (count >= 1)
System.out.println("No");
else
System.out.println("Yes");
return 0;
}
// Driver code.
public static void main(String[] args)
{
int m = 3, n = 3;
int s[][] = { { 1, 0, 1 },
{ 0, 0, 0 },
{ 1, 0, 0 } };
// Function calling
uniqueRows(s, m, n);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to check whether the
# Rows of Binary Matrix become unique
# After Deleting a column.
# Function to check whether rows of
# Binary matrix become unique after
# Deleting a column of from matrix.
def uniqueRows(s, m, n):
# Initialize variable count that
# stores the count of duplicate rows.
i, j, count = 0, 0, 0
# Take two nested loop and
# check weather rows already
# get seen then increment
# count by 1 then break the loop.
for i in range(n):
for j in range(i):
if (s[i] == s[j]):
count += 1
break
# Check if count>=1 then prNo
# Else prYes.
if (count >= 1):
print("No" )
else:
print("Yes")
# Driver code.
m = 3
n = 3
s = [[ 1, 0, 1 ],
[ 0, 0, 0 ],
[ 1, 0, 0 ]]
uniqueRows(s, m, n)
# This code is contributed by Mohit Kumar
C#
// C# iprogram to check whether the
// Rows of Binary Matrix become unique
// After Deleting a column.
using System;
class GFG
{
// Function to check whether rows of
// Binary matrix become unique after
// Deleting a column of from matrix.
static int uniqueRows(string []s,
int m, int n)
{
// Initialize variable count that
// stores the count of duplicate rows.
int i, j, count = 0;
// Take two nested loop and
// check weather rows already
// get seen then increment
// count by 1 then break the loop.
for (i = 0; i < n; i++)
{
for (j = 0; j < i; j++)
{
if (s[i] == s[j])
{
count++;
break;
}
}
}
// Check if count>=1 then print No
// Else print Yes.
if (count >= 1)
Console.WriteLine("No");
else
Console.WriteLine("Yes");
return 0;
}
// Driver code.
public static void Main(String[] args)
{
int m = 3, n = 3;
string []s = { "101","000", "100"};
// Function calling
uniqueRows(s, m, n);
}
}
// This code is contributed by Princi Singh
输出:
Yes
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。