SalesForce OA-7 2023
Problem Description
Alex is on an adventure and needs to cross a stone bridge where each stone is labeled with a random string of characters (S). He can only jump onto stones that are labeled with special characters, which are defined as any characters that are not letters (i.e., not in the ranges [A..Z] or [a..z]).
Your task is to determine the maximum length of a single jump Alex must make to cross the bridge. Each character on the bridge is spaced 1 unit apart. If there are no special characters on the bridge, return -1.
Input Format:
- A single string S of length n, represents the characters on the stones of the bridge.
Output Format:
- Return an integer representing the maximum length of a single jump Luffy has to take.
- If Luffy cannot cross the bridge (i.e., there are no special characters), return
-1
.
Constraints:
- 0 < n < 10^6 (i.e., the string length is up to 1,000,000).
Sample Testcases:
Sample Input 1:
S = "ABK@2azxy@gk"
Sample Output 1:
5
Explanation:
The special characters in the string are @
, 2
, and @
. Luffy jumps from the start to the first @
, then to 2
, and finally to the second @
. The longest jump distance is from z
to the second @
, which is 5 units.
Sample Input 2:
S = "ABCSD"
Sample Output 2:
-1
Explanation:
There are no special characters in this string, so Luffy cannot cross the bridge, and the output is -1
.