📜  给定矩阵中的唯一行数

📅  最后修改于: 2022-05-13 01:57:06.464000             🧑  作者: Mango

给定矩阵中的唯一行数

给定一个大小为N*M且包含小写英文字母的二维矩阵 arr,任务是找出给定矩阵中唯一行的数量。

例子:

朴素的方法:一一遍历所有行,并通过将每一行与所有其他行进行比较来检查它是否唯一。

时间复杂度: O(N 2 XM)
辅助空间: O(1)

有效的方法:使用 散列,我们可以将键存储为包含该行中所有字符的字符串,并将其频率存储为值。并遍历地图中的所有行,如果其频率为 1,则将其视为唯一。

下面是上述方法的实现:

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Function to find number of unique rows
int uniqueRows(vector > arr)
{
 
    // Map to store the frequency of
    // each row as string
    unordered_map mp;
    string t;
 
    for (auto x : arr) {
        t = "";
        for (auto y : x) {
            t += y;
        }
 
        mp[t] += 1;
    }
 
    int cnt = 0;
 
    // Counting unique rows
    for (auto x : mp) {
        if (x.second == 1) {
            cnt += 1;
        }
    }
 
    return cnt;
}
 
// Driver Code
int main()
{
    vector > arr = {
        { 'a', 'b', 'c', 'd' },
        { 'a', 'e', 'f', 'r' },
        { 'a', 'b', 'c', 'd' },
        { 'z', 'c', 'e', 'f' },
    };
 
    cout << uniqueRows(arr);
    return 0;
}


Java
// Java code to implement the above approach
import java.util.*;
class GFG{
 
  // Function to find number of unique rows
  static int uniqueRows(char[][] arr)
  {
 
    // Map to store the frequency of
    // each row as String
    HashMap mp = new HashMap<>();
    String t="";
 
    for (char []x : arr) {
      t = "";
      for (char y : x) {
        t += y;
      }
      if(mp.containsKey(t)) {
        mp.put(t, mp.get(t)+1);
      }
      else
        mp.put(t, 1);
    }
 
    int cnt = 0;
 
    // Counting unique rows
    for (Map.Entry x : mp.entrySet()) {
      if (x.getValue() == 1) {
        cnt += 1;
      }
    }
 
    return cnt;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    char[][] arr = {
      { 'a', 'b', 'c', 'd' },
      { 'a', 'e', 'f', 'r' },
      { 'a', 'b', 'c', 'd' },
      { 'z', 'c', 'e', 'f' },
    };
 
    System.out.print(uniqueRows(arr));
  }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python 3 code to implement the above approach
from collections import defaultdict
 
# Function to find number of unique rows
def uniqueRows(arr):
 
    # Map to store the frequency of
    # each row as string
    mp = defaultdict(int)
    t = ""
 
    for x in arr:
        t = ""
        for y in x:
            t += y
 
        mp[t] += 1
 
    cnt = 0
 
    # Counting unique rows
    for x in mp:
        if (mp[x] == 1):
            cnt += 1
 
    return cnt
 
# Driver Code
if __name__ == "__main__":
 
    arr = [
        ['a', 'b', 'c', 'd'],
        ['a', 'e', 'f', 'r'],
        ['a', 'b', 'c', 'd'],
        ['z', 'c', 'e', 'f'],
    ]
 
    print(uniqueRows(arr))
 
    # This code is contributed by ukasp.


C#
// C# code to implement the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
     
// Function to find number of unique rows
static int uniqueRows(char [,]arr)
{
     
    // Map to store the frequency of
    // each row as string
    Dictionary mp = new Dictionary();
           
    string t;
 
    for(int i = 0; i < arr.GetLength(0); i++)
    {
        t = "";
        for(int j = 0; j < arr.GetLength(1); j++)
        {
            t += arr[i, j];
        }
        if (mp.ContainsKey(t))
        {
            mp[t] = mp[t] + 1;
        }
        else
        {
            mp.Add(t, 1);
        }
    }
 
    int cnt = 0;
 
    // Counting unique rows
    foreach(var x in mp.Keys)
    {
        if (mp[x] == 1)
        {
            cnt += 1;
        }
    }
    return cnt;
}
 
// Driver Code
public static void Main()
{
    char [,]arr = { { 'a', 'b', 'c', 'd' },
                    { 'a', 'e', 'f', 'r' },
                    { 'a', 'b', 'c', 'd' },
                    { 'z', 'c', 'e', 'f' }, };
                     
    Console.Write(uniqueRows(arr));
}
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
2

时间复杂度: O(N*M)
辅助空间: O(N*M)