Frontend School

Profile Name Similarity

DSA
easy

Profile Name Similarity

Easy
String

Overview

LinkedIn's platform handles millions of user profiles, and identifying similar profiles is crucial for features like search, recommendations, and duplicate detection. A common heuristic for matching involves checking how 'close' two profile names are. This problem simulates a simplified version of such a check, focusing on character-level differences.

What you need to do / Task

Implement a function areNamesSimilar that takes two strings, name1 and name2, and determines if they are 'similar'. Two names are considered similar if they satisfy both of the following conditions:

  1. They have the exact same length.
  2. They differ by at most one character. That is, if you change at most one character in name1, it can become name2.

Your function should return true if the names are similar, and false otherwise.

Input & output

  • name1: A string representing the first profile name.
  • name2: A string representing the second profile name.
  • Returns boolean: true if name1 and name2 are similar according to the definition, false otherwise.

Clarifications

  • The comparison is case-sensitive. For example, 'A' is different from 'a'.
  • The strings will consist of printable ASCII characters.

Examples

1

Example 1

Input:

name1 = "LinkedIn", name2 = "LinkeDin"

Output:

true

Note:

The names have the same length (8) and differ by only one character ('e' vs 'D').

2

Example 2

Input:

name1 = "engineer", name2 = "engineers"

Output:

false

Note:

The names have different lengths (8 vs 9).

3

Example 3

Input:

name1 = "apple", name2 = "apply"

Output:

true

Note:

The names have the same length (5) and differ by only one character ('e' vs 'y').

4

Example 4

Input:

name1 = "hello", name2 = "world"

Output:

false

Note:

The names have the same length (5) but differ by more than one character (4 differences).

5

Example 5

Input:

name1 = "test", name2 = "test"

Output:

true

Note:

The names are identical, meaning zero differences.

Constraints

Constraints & assumptions

  • 1 <= name1.length, name2.length <= 100
  • name1 and name2 consist of printable ASCII characters.

What interviewers evaluate

  • Correctness in handling string length comparisons.
  • Efficient comparison of characters to count differences.
  • Clarity and readability of the code, demonstrating good coding practices.
  • Robustness in handling edge cases, such as identical strings or strings with multiple differences.

Loading editor...