String Similarity Check
Filed under Nagarro
Free read 2 of 3Confirm matching means identical character at index i (s1[i] == s2[i]).
The question
Given two strings s1 and s2 of equal length, determine if at least 50% of the corresponding positional characters match. Return True if the match ratio is >= 0.5, otherwise False.
Constraints
- 1 <= len(s1) == len(s2) <= 10^5
- s1 and s2 consist of printable ASCII characters.
Examples
- Input
- s1 = "apple", s2 = "appxx"
- Output
- True
- Why
- 3 out of 5 characters match at the same indices (60% >= 50%).
- Input
- s1 = "abcd", s2 = "abxx"
- Output
- True
- Why
- 2 out of 4 match (50% >= 50%).
- Input
- s1 = "cat", s2 = "dog"
- Output
- False
- Why
- 0 matches (0% < 50%).
Approach
Confirm matching means identical character at index i (s1[i] == s2[i]).
Iterate through both strings simultaneously and increment a match counter whenever s1[i] == s2[i].
Check if matches * 2 >= len(s1) to avoid floating-point issues.
Solution
O(n) time, O(1) space
class Solution:
def isSimilar(self, s1: str, s2: str) -> bool:
if len(s1) != len(s2):
return False
matches = sum(1 for c1, c2 in zip(s1, s2) if c1 == c2)
return matches * 2 >= len(s1)
Tips
- What they are testing: Tests basic string traversal, condition checking, and avoiding floating point precision pitfalls.
- Common mistake: Comparing multi-set frequencies instead of positional characters, or failing boundary conditions like exactly 50%.
Related Nagarro questions
Collected from public candidate posts and coding forums. Updated Nov 2026.