# 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 1^{st}and 2^{nd}strings are the largest.

Second digit of the string 2^{nd}is the largest.

Third digit of the string 1^{st}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++

`// 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*

*filter_none*

## 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

**Output:**

2

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

## Recommended Posts:

- Count all possible N digit numbers that satisfy the given condition
- Check whether two strings are equivalent or not according to given condition
- Number of common base strings for two strings
- Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions
- All combinations of strings that can be used to dial a number
- Count number of strings (made of R, G and B) using given combination
- Number of strings of length N with no palindromic sub string
- Count the number of common divisors of the given strings
- Remove minimum number of characters so that two strings become anagram
- Number of Binary Strings of length N with K adjacent Set Bits
- Minimum number of given operations required to make two strings equal
- Least number of manipulations needed to ensure two strings have identical characters
- Find all strings formed from characters mapped to digits of a number
- Largest sub-set possible for an array satisfying the given condition
- Check if the string satisfies the given condition

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.