Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Carl and Acceptable Strings Carl loves solving word puzzles. He is solving a complicated word puzzle and gets stuck at some point and asks you

Carl and Acceptable Strings
Carl loves solving word puzzles. He is solving a complicated word puzzle and gets stuck at some point and asks you
for your help.
The puzzle has a set of rules for a string to be considered acceptable. A string is considered acceptable if each of its
characters is one of the first K letters of the English alphabet, and none of its substrings is a palindrome (of length 2 or
more)
For example, the string "abcde" is considered acceptable for k=5 but not for k=4(Because 'e' is the 5 th letter of the
English alphabet). The string "aabc" is not acceptable because of the presence of "aa", which is a palindrome.
You are given an acceptable string s having only lowercase English alphabets. Your task is to return the next
lexicographically greater acceptable string that has the same length as the input string or return 'NO' if such a string
does not exist.
Note: A string r is considered lexicographically greater than string s of the same length, if there exists a number /
such that r1=s1,r2=s2,dots,r1=si,ri+1>si+1
Input Specification:
input1: an integer denoting K
input2: an input string s
Output Specification:
The next lexicographically greater acceptable string of the same length. Note: A string r is considered lexicographically greater than string s of the same length, if there exists a number i,
such that f1=s1,r2=s2,f1=s1,r101>sn+1
Input Specification:
input1: an integer denoting K
input2: an input string S
Output Specification:
The next lexicographically greater acceptable string of the same length.
Example 1:
input1: 5
input2: acbdac
Output: acbdae
Explanation:
The String "acbdae" is acceptable strings for k=5 as the letters it contains are among the first 5 letters of the Englost
aiphabet and none of their substrings are palindromes. So, "acbdae" will be returned as the answer.
Example 2:
input1: 3
input2: cba
Output: NO
Explanation:
There are no acceptable strings of length 3 for k=3 that are lexicographically greater than cba whose substrings a
not palindromes
Provide java code with correct output. The previously provided solution is not working
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

Concepts Of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

4th Edition

ISBN: 0619064625, 978-0619064624

More Books

Students also viewed these Databases questions

Question

=+What can we learn about the PVA data from this decision tree?

Answered: 1 week ago