Number of strings that satisfy the given condition - GeeksforGeeks

02 Jun, 2021
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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the count of valid stringsint 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 codeint main(){    string s[] = { "223", "232", "112" };    int m = s[0].length();    int n = sizeof(s) / sizeof(s[0]);    cout << countStrings(n, m, s);}
// Java implementation of the approachimport java.util.*; class GfG{ // Function to return the count of valid stringsstatic 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 codepublic 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 implementation of the approach # Function to return the count of# valid stringsdef 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 codes = ["223", "232", "112"]m = len(s[0])n = len(s)print(countStrings(n, m, s)) # This code is contributed# by Mohit Kumar
// C# implementation of the approachusing System;using System.Collections.Generic; class GfG{ // Function to return the count of valid stringsstatic 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 codepublic 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 */
<script> // JavaScript implementation of the approach // Function to return the count of valid stringsfunction countStrings(n,m,s){    // Set to store indices of valid strings    let ind = new Set();    for (let j = 0; j < m; j++)    {        let mx = 0;           // Find the maximum digit for current position        for (let i = 0; i < n; i++)            mx = Math.max(mx, (s[i][j].charCodeAt(0) -            '0'.charCodeAt(0)));           // Add indices of all the strings in the set        // that contain maximal digit        for (let i = 0; i < n; i++)            if (s[i][j].charCodeAt(0) - '0'.charCodeAt(0) == mx)                ind.add(i);    }       // Return number of strings in the set    return ind.size;} // Driver codelet s=[ "223", "232", "112"];let m = s[0].length;let n = s.length;document.write(countStrings(n, m, s)); // This code is contributed by patel2127 </script>
2
 
Time Complexity: O(N * M) where N is the number of strings and M is the length of the strings. 
mohit kumar 29
29AjayKumar
princiraj1992
patel2127
cpp-unordered_set
Competitive Programming
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Multistage Graph (Shortest Path)
Breadth First Traversal ( BFS ) on a 2D array
Shortest path in a directed graph by Dijkstra’s algorithm
5 Best Books for Competitive Programming
5 Best Languages for Competitive Programming
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Longest Common Subsequence | DP-4