Number of strings that satisfy the given condition
Last Updated :
22 Aug, 2023
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 the 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++
#include <bits/stdc++.h>
using namespace std;
int countStrings( int n, int m, string s[])
{
unordered_set< int > ind;
for ( int j = 0; j < m; j++) {
int mx = 0;
for ( int i = 0; i < n; i++)
mx = max(mx, ( int )s[i][j] - '0' );
for ( int i = 0; i < n; i++)
if (s[i][j] - '0' == mx)
ind.insert(i);
}
return ind.size();
}
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
import java.util.*;
class GfG
{
static int countStrings( int n, int m, String s[])
{
HashSet<Integer> ind = new HashSet<Integer>();
for ( int j = 0 ; j < m; j++)
{
int mx = 0 ;
for ( int i = 0 ; i < n; i++)
mx = Math.max(mx, ( int )(s[i].charAt(j) - '0' ));
for ( int i = 0 ; i < n; i++)
if (s[i].charAt(j) - '0' == mx)
ind.add(i);
}
return ind.size();
}
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));
}
}
|
Python3
def countStrings(n, m, s):
ind = dict ()
for j in range (m):
mx = 0
str1 = s[j]
for i in range (n):
mx = max (mx, int (str1[i]))
for i in range (n):
if int (str1[i]) = = mx:
ind[i] = 1
return len (ind)
s = [ "223" , "232" , "112" ]
m = len (s[ 0 ])
n = len (s)
print (countStrings(n, m, s))
|
C#
using System;
using System.Collections.Generic;
class GfG
{
static int countStrings( int n, int m, String[] s)
{
HashSet< int > ind = new HashSet< int >();
for ( int j = 0; j < m; j++)
{
int mx = 0;
for ( int i = 0; i < n; i++)
mx = Math.Max(mx, ( int )(s[i][j] - '0' ));
for ( int i = 0; i < n; i++)
if (s[i][j] - '0' == mx)
ind.Add(i);
}
return ind.Count;
}
public static void Main()
{
String []s = { "223" , "232" , "112" };
int m = s[0].Length;
int n = s.Length;
Console.WriteLine(countStrings(n, m, s));
}
}
|
Javascript
<script>
function countStrings(n,m,s)
{
let ind = new Set();
for (let j = 0; j < m; j++)
{
let mx = 0;
for (let i = 0; i < n; i++)
mx = Math.max(mx, (s[i][j].charCodeAt(0) -
'0' .charCodeAt(0)));
for (let i = 0; i < n; i++)
if (s[i][j].charCodeAt(0) - '0' .charCodeAt(0) == mx)
ind.add(i);
}
return ind.size;
}
let s=[ "223" , "232" , "112" ];
let m = s[0].length;
let n = s.length;
document.write(countStrings(n, m, s));
</script>
|
Time Complexity: O(N * M) where N is the number of strings and M is the length of the strings.
Auxiliary Space: O(N * M), for using set, where N is the number of strings and M is the length of the strings.
Approach: Iterative Index Position Comparison
This problem can be solved by iterating through each index position of the given strings and checking if the digit at that position is greater than the corresponding digit of all other strings. We can use a nested loop to compare each string with every other string.
Steps:
- Initialize a counter variable to keep track of the number of strings that satisfy the condition.
- Iterate through each index position of the strings from 0 to length-1.
- For each index position, iterate through each string and compare the digit at that index position with the corresponding digit of all other strings.
- If the digit is greater than all other digits at the same index position, increment the counter.
- Return the counter.
C++
#include <bits/stdc++.h>
using namespace std;
int count_strings(vector<string> arr)
{
int n = arr.size();
int m = arr[0].size();
int count = 0;
for ( int i = 0; i < m; i++) {
int max_digit = arr[0][i] - '0' ;
for ( int j = 1; j < n; j++) {
if (arr[j][i] - '0' > max_digit) {
max_digit = arr[j][i] - '0' ;
}
}
if (arr[0][i] - '0' == max_digit) {
count++;
}
}
return count;
}
int main()
{
vector<string> arr = { "223" , "232" , "112" };
int result = count_strings(arr);
cout << result << endl;
return 0;
}
|
Java
import java.util.ArrayList;
public class Main {
public static int countStrings(ArrayList<String> arr)
{
int n = arr.size();
int m = arr.get( 0 ).length();
int count = 0 ;
for ( int i = 0 ; i < m; i++) {
int maxDigit = arr.get( 0 ).charAt(i) - '0' ;
for ( int j = 1 ; j < n; j++) {
if (arr.get(j).charAt(i) - '0' > maxDigit) {
maxDigit = arr.get(j).charAt(i) - '0' ;
}
}
if (arr.get( 0 ).charAt(i) - '0' == maxDigit) {
count++;
}
}
return count;
}
public static void main(String[] args)
{
ArrayList<String> arr = new ArrayList<>();
arr.add( "223" );
arr.add( "232" );
arr.add( "112" );
int result = countStrings(arr);
System.out.println(result);
}
}
|
Python3
def count_strings(arr):
n = len (arr)
m = len (arr[ 0 ])
count = 0
for i in range (m):
max_digit = int (arr[ 0 ][i])
for j in range ( 1 , n):
if int (arr[j][i]) > max_digit:
max_digit = int (arr[j][i])
if int (arr[ 0 ][i]) = = max_digit:
count + = 1
return count
arr = [ "223" , "232" , "112" ]
result = count_strings(arr)
print (result)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int CountStrings(List< string > arr)
{
int n = arr.Count;
int m = arr[0].Length;
int count = 0;
for ( int i = 0; i < m; i++)
{
int maxDigit = arr[0][i] - '0' ;
for ( int j = 1; j < n; j++)
{
if (arr[j][i] - '0' > maxDigit)
{
maxDigit = arr[j][i] - '0' ;
}
}
if (arr[0][i] - '0' == maxDigit)
{
count++;
}
}
return count;
}
static void Main()
{
List< string > arr = new List< string > { "223" , "232" , "112" };
int result = CountStrings(arr);
Console.WriteLine(result);
}
}
|
Javascript
function countStrings(arr) {
let n = arr.length;
let m = arr[0].length;
let count = 0;
for (let i = 0; i < m; i++) {
let maxDigit = parseInt(arr[0][i]);
for (let j = 1; j < n; j++) {
if (parseInt(arr[j][i]) > maxDigit) {
maxDigit = parseInt(arr[j][i]);
}
}
if (parseInt(arr[0][i]) === maxDigit) {
count++;
}
}
return count;
}
let arr = [ "223" , "232" , "112" ];
let result = countStrings(arr);
console.log(result);
|
Time Complexity: The time complexity of this solution is O(N*M2), where N is the number of strings and M is the length of each string.
Auxiliary Space: The auxiliary space used by this solution is O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...