Number of strings that satisfy the given condition

Given N strings of equal lengths. The strings contain only digits (1 to 9). The task is to count the number of strings that have an index position such that the digit at this index position is greater than the digits at same index position of all the other strings.

Examples:

Input: arr[] = {“223”, “232”, “112”}
Output: 2
First digit of 1st and 2nd strings are the largest.
Second digit of the string 2nd is the largest.
Third digit of the string 1st is the largest.



Input: arr[] = {“999”, “122”, “111”}
Output: 1

Approach: For each index position, find the maximal digit in that position across all the strings. And store the indices of the string that satisfy the given condition in a set so that the same string isn’t counted twice for different index positions. Finally, return the size of the set.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
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<int> 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);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<Integer> ind = new HashSet<Integer>();
    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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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<int> ind = new HashSet<int>();
    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 */

chevron_right


Output:

2

Time Complexity: O(N * M) where N is the number of strings and M is the length of the strings.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.