Longest Cipher (Intuit India)
Learn this problemProblem statement
You are working on a secret government project, and you are tasked with decoding. You hit upon a theory that the cipher is hidden within a sequence of characters and has the following restrictions:
- (a) It has to contain at least one uppercase character.
- (b) It cannot contain any digits.
You are given a string s consisting of n alphanumerical characters. You need to find the longest substring of s that is a valid cipher and return its length. A substring is defined as a contiguous segment of a string.
For example, given "k3Cb", the substrings that are valid ciphers are "C" and "Cb". Note that "kCb" is not a substring, and "k3B" is not a valid cipher. In this case, your function should return 2 as the longest substring is "Cb". Alternatively, given "k3uu", your function should return -1 since there is no substring that satisfies the restrictions on the format of a valid cipher.
Input
The input contains a string s.
Output
Print the length of the longest substring that is a valid cipher.
If there is no such substring, your function should return -1.
Function
longestCipher(s: String) → intExamples
Example 1
s = "a2Bac"return = 3The three valid ciphers are {B, Ba, Bac}, so the output is the length of "Bac," which is 3.
Example 2
s = "aA0bCbd"return = 4The valid ciphers are {A, AE, bC, bCb, bCbd, C, Cb, Cbd}, so the output is 4.
Constraints
1 ≤ n ≤ 200s consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9).