Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Longest Cipher You are working on a secret government project and are tasked with decoding. You hit upon a theory that the cipher is hidden

Longest Cipher
You are working on a secret government project and 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 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.
Constraints
1n200
s consists only of alphanumerical characters and/or A-Z and/or 0-9).
Example #1
Input
a2Bac
Output
3
Explanation: The three valid ciphers are {B,Ba,Bac}, so the output is the length of "Bac," which is 3.
Example #2
Input
aAObCbd
Output
4
Explanation: The valid ciphers are ,bCbd,Cbd, so the output is 4.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

I am paid fairly for the work I do.

Answered: 1 week ago

Question

I receive the training I need to do my job well.

Answered: 1 week ago