📜  满足给定条件的字符串数

📅  最后修改于: 2021-06-27 01:39:10             🧑  作者: Mango

给定N个相等长度的字符串。字符串仅包含数字(1到9)。任务是计算具有索引位置的字符串的数量,以使该索引位置的数字大于所有其他字符串在相同索引位置的数字。

例子:

方法:对于每个索引位置,在所有字符串找到该位置的最大位数。并将满足给定条件的字符串的索引存储在一组中,这样同一字符串就不会为不同的索引位置计算两次。最后,返回集合的大小。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of valid strings
int countStrings(int n, int m, string s[])
{
  
    // Set to store indices of valid strings
    unordered_set ind;
    for (int j = 0; j < m; j++) {
        int mx = 0;
  
        // Find the maximum digit for current position
        for (int i = 0; i < n; i++)
            mx = max(mx, (int)s[i][j] - '0');
  
        // Add indices of all the strings in the set
        // that contain maximal digit
        for (int i = 0; i < n; i++)
            if (s[i][j] - '0' == mx)
                ind.insert(i);
    }
  
    // Return number of strings in the set
    return ind.size();
}
  
// Driver code
int main()
{
    string s[] = { "223", "232", "112" };
    int m = s[0].length();
    int n = sizeof(s) / sizeof(s[0]);
    cout << countStrings(n, m, s);
}


Java
// Java implementation of the approach
import java.util.*;
  
class GfG 
{
  
// Function to return the count of valid strings
static int countStrings(int n, int m, String s[])
{
  
    // Set to store indices of valid strings
    HashSet ind = new HashSet();
    for (int j = 0; j < m; j++)
    {
        int mx = 0;
  
        // Find the maximum digit for current position
        for (int i = 0; i < n; i++)
            mx = Math.max(mx, (int)(s[i].charAt(j) - '0'));
  
        // Add indices of all the strings in the set
        // that contain maximal digit
        for (int i = 0; i < n; i++)
            if (s[i].charAt(j) - '0' == mx)
                ind.add(i);
    }
  
    // Return number of strings in the set
    return ind.size();
}
  
// Driver code
public static void main(String[] args) 
{
    String s[] = { "223", "232", "112" };
    int m = s[0].length();
    int n = s.length;
    System.out.println(countStrings(n, m, s));
}
}
  
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
  
# Function to return the count of
# valid strings
def countStrings(n, m, s):
  
    # Set to store indices of
    # valid strings
    ind = dict()
    for j in range(m):
        mx = 0
  
        str1 = s[j]
  
        # Find the maximum digit for 
        # current position
        for i in range(n):
            mx = max(mx, int(str1[i]))
  
        # Add indices of all the strings in 
        # the set that contain maximal digit
        for i in range(n):
            if int(str1[i]) == mx:
                ind[i] = 1
      
    # Return number of strings 
    # in the set
    return len(ind)
  
# Driver code
s = ["223", "232", "112"]
m = len(s[0])
n = len(s)
print(countStrings(n, m, s))
  
# This code is contributed 
# by Mohit Kumar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
  
class GfG 
{
  
// Function to return the count of valid strings
static int countStrings(int n, int m, String[] s)
{
  
    // Set to store indices of valid strings
    HashSet ind = new HashSet();
    for (int j = 0; j < m; j++)
    {
        int mx = 0;
  
        // Find the maximum digit for current position
        for (int i = 0; i < n; i++)
            mx = Math.Max(mx, (int)(s[i][j] - '0'));
  
        // Add indices of all the strings in the set
        // that contain maximal digit
        for (int i = 0; i < n; i++)
            if (s[i][j] - '0' == mx)
                ind.Add(i);
    }
  
    // Return number of strings in the set
    return ind.Count;
}
  
// Driver code
public static void Main() 
{
    String []s = { "223", "232", "112" };
    int m = s[0].Length;
    int n = s.Length;
    Console.WriteLine(countStrings(n, m, s));
}
}
  
/* This code contributed by PrinciRaj1992 */


输出:
2

时间复杂度:O(N * M),其中N是字符串的数目,M是字符串的长度。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。