mediumHashingPattern: Hashing

Galaxy Signal Decoding Solution

Problem Statement

In a distant galaxy, signals are transmitted as strings of characters. Given a signal string and a scanner width, find the length of the first segment where all characters are unique. If no such segment exists within the given signal, return -1.

Examples

Example 1:
Input:{"signal":"abcdefghijklmnopqrstuvwxyz","scannerWidth":5}
Output:5
Explanation: The first 5 characters 'abcde' are all unique, so the function returns 5.
Example 2:
Input:{"signal":"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz","scannerWidth":27}
Output:-1
Explanation: Since the scanner width is 27 and there are only 26 unique characters in the alphabet, no segment of length 27 with all unique characters exists, so the function returns -1.

Constraints

  • 1 <= scannerWidth <= 100
  • 1 <= signal length <= 1000
  • signal consists of lowercase English letters only
Time: O(n) Space: O(min(n, m))
An optimized approach uses a sliding window technique in conjunction with a set to track unique characters within the current window, allowing for a linear time complexity. This method efficiently scans the signal string and identifies the first segment with all unique characters.

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.