easyStringsPattern: Two Pointers

Verify Nickname Subsequence Solution

Problem Statement

Given two strings, fullName and nickname, determine if nickname is a valid subsequence of fullName. A nickname is considered a valid subsequence if it can be derived from fullName by deleting zero or more characters without changing the relative order of the remaining characters. The comparison must be case-insensitive.

Examples

Example 1:
Input:fullName = "Benjamin", nickname = "Bnj"
Output:true
Explanation: The characters 'B', 'n', and 'j' appear in "Benjamin" in the same relative order (B-e-n-j-a-m-i-n) when ignoring case.
Example 2:
Input:fullName = "Sophia", nickname = "Osa"
Output:false
Explanation: Although 'o', 's', and 'a' exist in "Sophia", their relative order is not preserved ('s' comes before 'o' in "Sophia", whereas 'O' comes before 's' in "Osa").

Constraints

  • 1 <= fullName.length <= 10^4
  • 1 <= nickname.length <= 10^4
  • fullName and nickname consist only of uppercase and lowercase English letters.
Time: O(n + m) Space: O(1)
The optimal approach is to use two pointers, one for the fullName string and one for the nickname string, to compare characters in order. This approach allows us to find a subsequence in linear time.

Run, Test & Submit Code

Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.

Solve on Interactive Workspace

Tested Solutions

No solution code is currently loaded.
Complete this code in the workspace editor.