Skip to main content

Salesforce

SalesForce OA 2023 - Luffy Adventure to Grand Line

Problem Description:

Luffy is on an adventure and needs to cross a stone bridge to reach the Grand Line. Each stone on the bridge is labeled with a random string of characters ( S ). Luffy can only jump onto stones labeled with special characters; otherwise, he falls into the ocean. The special characters are any characters other than [A..Z] and [a..z] (i.e., digits, punctuation, etc., are considered special).

Luffy wants to find out the maximum length of a single jump that he needs to take in order to cross the bridge. Each character on the bridge is at a distance of 1 unit. If Luffy cannot cross the bridge because there are no special characters, return -1.


Input Format:

  • A single string S of length n, representing 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.