Longest Cipher (Intuit India)
Updated
Problem 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 ≤ 200sconsists only of alphanumerical characters (a-z and/or A-Z and/or 0-9).
More Intuit interview questions
- Number of IslandsONSITE INTERVIEW · Seen Nov 2026
- VIP and Regular Service SchedulerPHONE SCREEN · Seen Nov 2026
- Employee Promotion OpportunitiesOA · Seen Oct 2026
- Next Pascal Row CoefficientOA · Seen Oct 2026
- Parse Router LogsOA · Seen Oct 2026
- Longest Repeated Letter Block with Adjacent SwapsOA · Seen Oct 2026
- Maximum Square-Free DivisorOA · Seen Oct 2026
- Two Tribes from Taller-Person CountsOA · Seen Oct 2026
- Shortest Path from an Encoded GraphOA · Seen Sep 2026
- Oldest and Youngest EmployeesOA · Seen Sep 2026
Similar questions at other companies
- Count Repeated Request IDs (Data Science OA)McKinsey & Company · OAMcKinsey & Company interview questions
- Find Sliding-Window Pattern Start IndicesVisa · OAVisa interview questions
- Get Special Substring (MTS)Salesforce · OASalesforce interview questions
- Find RepetitionsPure Storage · OAPure Storage interview questions
- Incident MonitorStripe · OAStripe interview questions
- Maximum Valid Substring FrequencyMicrosoft · OAMicrosoft interview questions
- Perfect SubstringAXQ Capital · OA
- Same Substring Within BudgetJP Morgan Chase · OAJP Morgan Chase interview questions
- Longest Distinct StringSuperhuman · OASuperhuman interview questions
- K Smallest SubstringAkuna Capital · OAAkuna Capital interview questions