Common Alphabets N Strings

N string values are passed as input to the program. Each string will contain only the alphabets a-z in lower case. A given alphabet may be repeated any number of times. The program must print the count C of the alphabets that are present in all the N string values.

Input Format:
The first line contains N.
Next N lines contain the N string values.

Output Format:
The first line contains C.

Boundary Conditions:
2 <N <500
1 <Length of the string value <1000

Example Input/Output 1:
Input:
3
mnppqqr
ajkmnnm
poormanagement
Output:
2
Explanation :
Only 2 alphabets m and n are present in all the three string values.

#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int n;
cin >> n;
string a[n],temp;
for(int i=0;i<n;i++)
{
    cin >> a[i];
}
int b[n][26]={0},c;
for(int i=0;i<n;i++)
{
    temp = a[i];
    for(int j=0;j<temp.length();j++)
    {
        c = temp[j]-97;
        b[i][c]++;
    }
}
int flag,count = 0;
for(int i=0;i<26;i++)
{
    flag = 1;
    for(int j=0;j<n;j++)
    {
        if(b[j][i] <= 0)
        {
            flag = 0;
        }
    }
    if(flag == 1)
    {
        count++;
    }
}
cout << count;
return 0;
}
n = int(input())  # Read the number of strings (N).

letter_count = [0] * 26  # Initialize an array to count occurrences of each letter.

for _ in range(n):
    s = input()  # Read each string.

    # Create a set to track which letters are present in the current string.
    present = set()

    # Mark each letter as present in the current string.
    for c in s:
        present.add(c)

    # Update the letter_count list based on the current string.
    for i in range(26):
        if chr(ord('a') + i) in present:
            letter_count[i] += 1

count = 0  # Initialize the count of letters present in all strings.

# Count the letters that are present in all strings.
for i in range(26):
    if letter_count[i] == n:
        count += 1

print(count)  # Output the count.
#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n); // Read the number of strings (N).

    int letterCount[26] = {0}; // Initialize an array to count occurrences of each letter.

    for (int i = 0; i < n; i++) {
        char s[1000];
        scanf("%s", s); // Read each string.

        // Create an array to track which letters are present in the current string.
        int present[26] = {0};

        // Mark each letter as present in the current string.
        for (int j = 0; s[j] != ''; j++) {
            present[s[j] - 'a'] = 1;
        }

        // Update the letterCount array based on the current string.
        for (int j = 0; j < 26; j++) {
            if (present[j]) {
                letterCount[j]++;
            }
        }
    }

    int count = 0; // Initialize the count of letters present in all strings.

    // Count the letters that are present in all strings.
    for (int i = 0; i < 26; i++) {
        if (letterCount[i] == n) {
            count++;
        }
    }

    printf("%dn", count); // Output the count.
    return 0;
}
using System;

class Program {
    static void Main() {
        int n = int.Parse(Console.ReadLine()); // Read the number of strings (N).

        int[] letterCount = new int[26]; // Initialize an array to count occurrences of each letter.

        for (int i = 0; i < n; i++) {
            string s = Console.ReadLine(); // Read each string.

            // Create an array to track which letters are present in the current string.
            bool[] present = new bool[26];

            // Mark each letter as present in the current string.
            foreach (char c in s) {
                present[c - 'a'] = true;
            }

            // Update the letterCount array based on the current string.
            for (int j = 0; j < 26; j++) {
                if (present[j]) {
                    letterCount[j]++;
                }
            }
        }

        int count = 0; // Initialize the count of letters present in all strings.

        // Count the letters that are present in all strings.
        for (int i = 0; i < 26; i++) {
            if (letterCount[i] == n) {
                count++;
            }
        }

        Console.WriteLine(count); // Output the count.
    }
}
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n; // Read the number of strings (N).

    int letterCount[26] = {0}; // Initialize an array to count occurrences of each letter.

    for (int i = 0; i < n; i++) {
        string s;
        cin >> s; // Read each string.

        // Create an array to track which letters are present in the current string.
        bool present[26] = {false};

        // Mark each letter as present in the current string.
        for (char c : s) {
            present[c - 'a'] = true;
        }

        // Update the letterCount array based on the current string.
        for (int j = 0; j < 26; j++) {
            if (present[j]) {
                letterCount[j]++;
            }
        }
    }

    int count = 0; // Initialize the count of letters present in all strings.

    // Count the letters that are present in all strings.
    for (int i = 0; i < 26; i++) {
        if (letterCount[i] == n) {
            count++;
        }
    }

    cout << count << endl; // Output the count.
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.