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++
// 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); } |
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<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 |
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< 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 */ |
Javascript
<script> // JavaScript implementation of the approach // Function to return the count of valid strings function 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 code let 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.
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; // Function to count the number of strings int count_strings(vector<string> arr) { int n = arr.size(); int m = arr[0].size(); int count = 0; // Traverse the array of strings for ( int i = 0; i < m; i++) { int max_digit = arr[0][i] - '0' ; // Traverse each strings 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++; } } // Print the resultant count return count; } // Driver Code int main() { vector<string> arr = { "223" , "232" , "112" }; // Call the function and print the result 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 ; // Traverse the array of strings for ( int i = 0 ; i < m; i++) { int maxDigit = arr.get( 0 ).charAt(i) - '0' ; // Traverse each strings 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 the resultant count return count; } // Driver Code public static void main(String[] args) { ArrayList<String> arr = new ArrayList<>(); arr.add( "223" ); arr.add( "232" ); arr.add( "112" ); // Call the function and print the result int result = countStrings(arr); System.out.println(result); } } |
Python3
# Python program for the above approach # Function to count the number of strings def count_strings(arr): n = len (arr) m = len (arr[ 0 ]) count = 0 # Traverse the array of strings for i in range (m): max_digit = int (arr[ 0 ][i]) # Traverse each strings 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 # Print the resultant count return count # Driver Code arr = [ "223" , "232" , "112" ] # Call the function and print the result result = count_strings(arr) print (result) |
C#
using System; using System.Collections.Generic; class GFG { // Function to count the number of strings static int CountStrings(List< string > arr) { int n = arr.Count; int m = arr[0].Length; int count = 0; // Traverse the array of strings for ( int i = 0; i < m; i++) { int maxDigit = arr[0][i] - '0' ; // Traverse each string 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 the resultant count return count; } // Driver Code static void Main() { List< string > arr = new List< string > { "223" , "232" , "112" }; // Call the function and print the result int result = CountStrings(arr); Console.WriteLine(result); } } |
Javascript
// Function to count the number of strings function countStrings(arr) { let n = arr.length; let m = arr[0].length; let count = 0; // Traverse the array of strings for (let i = 0; i < m; i++) { let maxDigit = parseInt(arr[0][i]); // Traverse each strings 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++; } } // Print the resultant count return count; } // Driver Code let arr = [ "223" , "232" , "112" ]; // Call the function and print the result let result = countStrings(arr); console.log(result); // This code is contributed by shivamgupta0987654321 |
2
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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!