Removing row or column wise duplicates from matrix of characters
Last Updated :
18 Nov, 2022
Given a matrix of characters containing only lowercase letters, form a new string from the matrix that remains after removing the duplicate characters from each row and column. The remaining characters are joined row-wise to form the string.
Examples:
Input : Matrix of characters (Or array
of strings)
aabcd
effgh
iijkk
lmnoo
pqqrs
ttuvw
xxyyz
Output : bcdeghjlmnprsuvwz
The characters that appear more than once
in their row or column are removed.
Input : zx
xz
Output :zxxz
Approach: Traverse the entire matrix and for every element, check its corresponding row and column. If there are duplicates, then mark that particular cell in another matrix as true. Finally, all the characters for which there is a false value in another matrix are joined to form the string.
C++
#include <bits/stdc++.h>
using namespace std;
void findDuplicates(string a[], int n, int m)
{
bool isPresent[n][m];
memset (isPresent, 0, sizeof (isPresent));
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
for ( int k = 0; k < n; k++) {
if (a[i][j] == a[k][j] && i != k) {
isPresent[i][j] = true ;
isPresent[k][j] = true ;
}
}
for ( int k = 0; k < m; k++) {
if (a[i][j] == a[i][k] && j != k) {
isPresent[i][j] = true ;
isPresent[i][k] = true ;
}
}
}
}
for ( int i = 0; i < n; i++)
for ( int j = 0; j < m; j++)
if (!isPresent[i][j])
printf ( "%c" , a[i][j]);
}
int main()
{
int n = 2, m = 5;
string a[] = { "zx" , "xz" };
findDuplicates(a, n, m);
return 0;
}
|
Java
class GFG
{
static void findDuplicates(String []a,
int n, int m)
{
boolean [][]isPresent = new boolean [n] [m];
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < m; j++)
{
isPresent[i][j]= false ;
}
}
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < m; j++)
{
for ( int k = 0 ; k < n; k++)
{
if (a[i].charAt(j)== a[k].charAt(j) &&
i != k)
{
isPresent[i][j] = true ;
isPresent[k][j] = true ;
}
}
for ( int k = 0 ; k < m; k++)
{
if (a[i].charAt(j)== a[i].charAt(k) &&
j != k)
{
isPresent[i][j] = true ;
isPresent[i][k] = true ;
}
}
}
}
for ( int i = 0 ; i < n; i++)
for ( int j = 0 ; j < m; j++)
if (isPresent[i][j]== false )
System.out.print(a[i].charAt(j));
}
public static void main(String []args)
{
int n = 2 , m = 2 ;
String []a = new String[]{ "zx" ,
"xz" };
findDuplicates(a, n, m);
}
}
|
Python3
def findDuplicates(a, n, m):
isPresent = [[ False for i in range (n)]
for j in range (m)]
for i in range (n):
for j in range (m):
for k in range (n):
if i ! = k and a[i][j] = = a[k][j]:
isPresent[i][j] = True
isPresent[k][j] = True
for k in range (m):
if j ! = k and a[i][j] = = a[i][k]:
isPresent[i][j] = True
isPresent[i][k] = True
for i in range (n):
for j in range (m):
if not isPresent[i][j]:
print (a[i][j], end = "")
if __name__ = = "__main__" :
n = 2
m = 2
a = [ "zx" , "xz" ]
findDuplicates(a, n, m)
|
C#
using System;
class GFG
{
static void findDuplicates( string []a,
int n, int m)
{
bool [,]isPresent = new bool [n, m];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
for ( int k = 0; k < n; k++)
{
if (a[i][j] == a[k][j] &&
i != k)
{
isPresent[i, j] = true ;
isPresent[k, j] = true ;
}
}
for ( int k = 0; k < m; k++)
{
if (a[i][j] == a[i][k] &&
j != k)
{
isPresent[i, j] = true ;
isPresent[i, k] = true ;
}
}
}
}
for ( int i = 0; i < n; i++)
for ( int j = 0; j < m; j++)
if (!isPresent[i, j])
Console.Write(a[i][j]);
}
static void Main()
{
int n = 2, m = 2;
string []a = new string []{ "zx" ,
"xz" };
findDuplicates(a, n, m);
}
}
|
Javascript
<script>
function findDuplicates( a , n , m) {
var isPresent = Array(n).fill().map(() => Array(m).fill(0));
for ( var i = 0; i < n; i++) {
for ( var j = 0; j < m; j++) {
isPresent[i][j] = false ;
}
}
for ( var i = 0; i < n; i++) {
for ( var j = 0; j < m; j++) {
for ( var k = 0; k < n; k++) {
if (a[i].charAt(j) == a[k].charAt(j) && i != k) {
isPresent[i][j] = true ;
isPresent[k][j] = true ;
}
}
for (k = 0; k < m; k++) {
if (a[i].charAt(j) == a[i].charAt(k) && j != k) {
isPresent[i][j] = true ;
isPresent[i][k] = true ;
}
}
}
}
for ( var i = 0; i < n; i++)
for ( var j = 0; j < m; j++)
if (isPresent[i][j] == false )
document.write(a[i].charAt(j));
}
var n = 2, m = 2;
var a = [ "zx" , "xz" ];
findDuplicates(a, n, m);
</script>
|
Time Complexity: O(n * m * (n + m)), where n and m are the numbers of rows and columns.
Auxiliary Space: O(n * m), where n and m are the numbers of rows and columns.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...