Common Characters in Array of Strings

Find characters common to all strings.

Logic BuildingAdvanced
Logic Building
# Take array of strings
n = int(input("Enter number of strings: "))
arr = []
for i in range(n):
    arr.append(input(f"String {i+1}: "))

# Find common characters
if arr:
    common = set(arr[0])
    for s in arr[1:]:
        common = common & set(s)
    
    print(f"Common characters: {sorted(common)}")
else:
    print("Array is empty")

Output

Enter number of strings: 3
String 1: hello
String 2: world
String 3: level
Common characters: ['e', 'l']

Use set intersection to find common characters.

Key Concepts:

  • Convert each string to set
  • Use & operator for intersection
  • Find common characters