Common Characters – Same Position

The program must accept N string values are of equal length as the input. The program must print the common characters at the same position in all the N string values as the output.

Note:
At least one common character at the same position is always present in the N string values.

Boundary Condition(s):
2 <= N <= 100
1 <= Length of S <= 1000

Input Format:
The first line contains N.
The next N lines each contain a string value.

Output Format:
The first line contains the common characters.

Example Input/Output 1:
Input:
3
next
best
test
Output:
et

Explanation:
Here e and t are the common characters present at the same position in the three string values. Hence the output is et

Example Input/Output 2:
Input:
4
holding
waiting
walking
selling
Output:
ing

n = int(input())
strings = [input().strip() for _ in range(n)]

for j in range(len(strings[0])):
    if all(s[j] == strings[0][j] for s in strings):
        print(strings[0][j], end="")
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

int main() {
    int n;
    scanf("%d", &n);

    char strings[n][1001];  // Given max length of string is 1000
    for (int i = 0; i < n; i++) {
        scanf("%s", strings[i]);
    }

    int length = strlen(strings[0]);
    for (int j = 0; j < length; j++) {
        bool same = true;
        for (int i = 1; i < n; i++) {
            if (strings[i][j] != strings[0][j]) {
                same = false;
                break;
            }
        }
        if (same) {
            printf("%c", strings[0][j]);
        }
    }

    return 0;
}
#include <iostream>
#include <vector>
#include <string>

int main() {
    int n;
    std::cin >> n;

    std::vector<std::string> strings(n);
    for (int i = 0; i < n; i++) {
        std::cin >> strings[i];
    }

    for (int j = 0; j < strings[0].size(); j++) {
        bool same = true;
        for (int i = 1; i < n; i++) {
            if (strings[i][j] != strings[0][j]) {
                same = false;
                break;
            }
        }
        if (same) {
            std::cout << strings[0][j];
        }
    }

    return 0;
}
import java.util.Scanner;

public class CommonCharacters {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        String[] strings = new String[n];

        for (int i = 0; i < n; i++) {
            strings[i] = scanner.next();
        }

        for (int j = 0; j < strings[0].length(); j++) {
            boolean same = true;
            for (int i = 1; i < n; i++) {
                if (strings[i].charAt(j) != strings[0].charAt(j)) {
                    same = false;
                    break;
                }
            }
            if (same) {
                System.out.print(strings[0].charAt(j));
            }
        }
    }
}
Previous Article

Pattern Printing - Diamond Numbers

Next Article

CopyAll and Paste Operations

Write a Comment

Leave a Comment

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