A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python-count-the-number-of-matching-characters-in-a-pair-of-string/ below:

Python | Count the Number of matching characters in a pair of string

Python | Count the Number of matching characters in a pair of string

Last Updated : 11 Jul, 2025

The problem is about finding how many characters are the same in two strings. We compare the strings and count the common characters between them. In this article, we'll look at different ways to solve this problem.

Using Set

Sets are collections of unique items, so by converting both strings into sets, we can easily find the common characters between them. This method is efficient because sets quickly identify matches.

Example:

Python
s1 = "VISHAKSHI"
s2 = "VANSHIKA"

# find common characters
res= len(set(s1.lower()).intersection(set(s2.lower())))
print(res)

Explanation:

Using Counter

Counter that helps to count how many times each item appears in a list or string. It's useful for tasks like finding out how many characters are shared between two strings.

Example:

Python
from collections import Counter

s1 = "VISHAKSHI"
s2 = "VANSHIKA"

# Count characters
c1 = Counter(s1.lower())
c2= Counter(s2.lower())

# Find common characters and sum their counts
res = sum((c1 & c2).values())
print(res)

Explanation:

Using List Comprehension

This method checks which characters are found in both strings by turning them into sets. It then counts how many characters are the same in both strings.

Python
s1 = "VISHAKSHI"
s2 = "VANSHIKA"

# Find common characters
res= len([char for char in set(s1.lower())
          if char in set(s2.lower())])
print(res)

Explanation:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4