Program to display maximum consecutive increasingly ordered substring
Program Plan:
- Create the class “Exercise22_01”.
- In the main() function,
- Read the input object to read the input from user.
- Call the function maxConsecutivesSortedSubstring()to print the maximum ordered substring.
- In the maxConsecutivesSortedSubstring(),
- Initially assign the maximum length of the substring.
- Use for() loop to execute the whole length of the string.
- Check the position of character at “i” and “i-1” and compare the values.
- If it is lesser, assign the “i” to “current”.
- If the condition not satisfies, then increment the length of maximum consecutive length.
- Use for() loop to iterate the string values.
- Check whether the current length of string is less than the largest string.
- If yes, then assign the current element length to largest subsequence element.
- Construct the character sequence using while() loop at the end of the string.
- Return the string.
- Check the position of character at “i” and “i-1” and compare the values.
- In the maxConsecutivesSortedSubstring1(),
- Use the for loop to iterate the whole length of string.
- Check the position of character at “i” and “i-1” and compare the values.
- Return the sorted substring.
- Use the for loop to iterate the whole length of string.
This program reads the input from user and displays the maximum consecutive increasingly ordered substring.
Program:
//Declare the class Exercise22_01
public class Exercise22_01 {
//Declare the main function
public static void main(String[] args) {
//Create the input object
java.util.Scanner input = new
java.util.Scanner(System.in);
//Read the string
System.out.print("Enter a string: ");
String s = input.nextLine();
/*Call the function maxConsecutiveSortedSubstring() to print the maximum consecutive sorted substring*/
System.out.println("Maximum consecutive substring
is " + maxConsecutiveSortedSubstring(s));
}
/*Define the function maxConsecutiveSortedSubstring()*/
public static String
maxConsecutiveSortedSubstring(String s) {
//Declare the array and assign the length of array
int[] maxConsecutiveLength = new int[s.length()];
//Assign the variable as 0
int current = 0;
//Execute the for loop until the condition fails
for (int i = 1; i < s.length(); i++) {
/*Check whether the character is smaller than
the current character stored in string*/
if (s.charAt(i) <= s.charAt(i - 1)) {
//Assign the “i” value to current variable
current = i;
} else {
/*Execute the for loop until the condition fails*/
for (int j = i - 1; j >= current; j--)
//Increment the sequence of length
maxConsecutiveLength[j]++;
}
}
//Assign the length of sequence at index “i”
int currentMaxLength = maxConsecutiveLength[0];
int index = 0;
//Execute the for loop until the length of string
for (int i = 0; i < s.length(); i++) {
//Check whether the condition is true
if (maxConsecutiveLength[i] > currentMaxLength)
{
/*Assign the maximum sequence length to current length */
currentMaxLength = maxConsecutiveLength[i];
//Assign the index position
index = i;
}
}
//Return the substring
return s.substring(index, index + currentMaxLength + 1);
}
//Define the function for the sorted substring
public static String
maxConsecutiveSortedSubstring1(String s) {
//Assign the current length
int currentMaxLength = 1;
//Assign the last index of sorted substring
int lastIndexOfMaxConsecutiveSortedSubstring = 0;
//Assign the possible length
int possibleMaxLength = 1;
//Execute the for loop until it fails
for (int i = 1; i < s.length(); i++) {
//Check the position of the string
if (s.charAt(i) > s.charAt(i - 1)) {
//Check the condition
if
(lastIndexOfMaxConsecutiveSortedSubstri
ng == i - 1) {
/* Add the max consecutive substring*/
currentMaxLength++;
/*Add the index of max consecutive substring*/
lastIndexOfMaxConsecutiveSortedSub
string++;
//If condition not satisfies
} else {
//Increment the length
possibleMaxLength++;
//Check the condition
if (possibleMaxLength >
currentMaxLength) {
/*Assign the possible length to current maximum length*/
currentMaxLength =
possibleMaxLength;
/*Assign the index of the string*/
lastIndexOfMaxConsecutiveSort
edSubstring = i;
possibleMaxLength = 1;
}
}
}
}
//Return the sorted substring
return
s.substring(lastIndexOfMaxConsecutiveSortedSubstr
ing - currentMaxLength + 1,lastIndexOfMaxConsecutiveSortedSubstring + 1);
}
}
Running time complexity:
The above program executes in
Sample Output:
Enter a string: abcabcdgabmnsxy
Maximum consecutive substring is abmnsxy
Program to display maximum consecutive increasingly ordered substring
Program Plan:
- Create the class “Exercise22_01”.
- In the main() function,
- Read the input object to read the input from user.
- Call the function maxConsecutivesSortedSubstring()to print the maximum ordered substring.
- In the maxConsecutivesSortedSubstring(),
- Initially assign the maximum length of the substring.
- Use for() loop to execute the whole length of the string.
- Check the position of character at “i” and “i-1” and compare the values.
- If it is lesser, assign the “i” to “current”.
- If the condition not satisfies, then increment the length of maximum consecutive length.
- Use for() loop to iterate the string values.
- Check whether the current length of string is less than the largest string.
- If yes, then assign the current element length to largest subsequence element.
- Construct the character sequence using while() loop at the end of the string.
- Return the string.
- Check the position of character at “i” and “i-1” and compare the values.
- In the maxConsecutivesSortedSubstring1(),
- Use the for loop to iterate the whole length of string.
- Check the position of character at “i” and “i-1” and compare the values.
- Return the sorted substring.
- Use the for loop to iterate the whole length of string.
This program reads the input from user and displays the maximum consecutive increasingly ordered substring.
Explanation of Solution
Program:
//Declare the class Exercise22_01
public class Exercise22_01 {
//Declare the main function
public static void main(String[] args) {
//Create the input object
java.util.Scanner input = new
java.util.Scanner(System.in);
//Read the string
System.out.print("Enter a string: ");
String s = input.nextLine();
/*Call the function maxConsecutiveSortedSubstring() to print the maximum consecutive sorted substring*/
System.out.println("Maximum consecutive substring
is " + maxConsecutiveSortedSubstring(s));
}
/*Define the function maxConsecutiveSortedSubstring()*/
public static String
maxConsecutiveSortedSubstring(String s) {
//Declare the array and assign the length of array
int[] maxConsecutiveLength = new int[s.length()];
//Assign the variable as 0
int current = 0;
//Execute the for loop until the condition fails
for (int i = 1; i < s.length(); i++) {
/*Check whether the character is smaller than
the current character stored in string*/
if (s.charAt(i) <= s.charAt(i - 1)) {
//Assign the “i” value to current variable
current = i;
} else {
/*Execute the for loop until the condition fails*/
for (int j = i - 1; j >= current; j--)
//Increment the sequence of length
maxConsecutiveLength[j]++;
}
}
//Assign the length of sequence at index “i”
int currentMaxLength = maxConsecutiveLength[0];
int index = 0;
//Execute the for loop until the length of string
for (int i = 0; i < s.length(); i++) {
//Check whether the condition is true
if (maxConsecutiveLength[i] > currentMaxLength)
{
/*Assign the maximum sequence length to current length */
currentMaxLength = maxConsecutiveLength[i];
//Assign the index position
index = i;
}
}
//Return the substring
return s.substring(index, index + currentMaxLength + 1);
}
//Define the function for the sorted substring
public static String
maxConsecutiveSortedSubstring1(String s) {
//Assign the current length
int currentMaxLength = 1;
//Assign the last index of sorted substring
int lastIndexOfMaxConsecutiveSortedSubstring = 0;
//Assign the possible length
int possibleMaxLength = 1;
//Execute the for loop until it fails
for (int i = 1; i < s.length(); i++) {
//Check the position of the string
if (s.charAt(i) > s.charAt(i - 1)) {
//Check the condition
if
(lastIndexOfMaxConsecutiveSortedSubstri
ng == i - 1) {
/* Add the max consecutive substring*/
currentMaxLength++;
/*Add the index of max consecutive substring*/
lastIndexOfMaxConsecutiveSortedSub
string++;
//If condition not satisfies
} else {
//Increment the length
possibleMaxLength++;
//Check the condition
if (possibleMaxLength >
currentMaxLength) {
/*Assign the possible length to current maximum length*/
currentMaxLength =
possibleMaxLength;
/*Assign the index of the string*/
lastIndexOfMaxConsecutiveSort
edSubstring = i;
possibleMaxLength = 1;
}
}
}
}
//Return the sorted substring
return
s.substring(lastIndexOfMaxConsecutiveSortedSubstr
ing - currentMaxLength + 1,lastIndexOfMaxConsecutiveSortedSubstring + 1);
}
}
Running time complexity:
The above program executes in
is
Enter a string: abcabcdgabmnsxy
Maximum consecutive substring is abmnsxy
Want to see more full solutions like this?
Chapter 22 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Elementary Surveying: An Introduction To Geomatics (15th Edition)
Management Information Systems: Managing The Digital Firm (16th Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
Thinking Like an Engineer: An Active Learning Approach (4th Edition)
SURVEY OF OPERATING SYSTEMS
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- Rewrite the client program to use the Baby class that you have done using ArrayList. You should also have the following function in this new client program: a) Enter details for each baby (name and age) and thus populate the Baby ArrayList b) Calculate and display the average age of all babies in the ArrayList c) Ask the user to enter the name of a baby, and then remove it from the ArrayList d) Allow the user to add new baby information in the ArrayList e) Output the details of each baby from the ArrayList (name and age) Compare this client program using the ArrayList with the client program you have done using array, comment on the differences between the two. Modify your client program in the last question, by adding a sort function such that when that option is selected, it sorts the ArrayList using the baby names in alphabetical order and output the details in the sorted ArrayList to a CSV (sortedbabylist.csv) file. public class Baby { private String name; private int age;…arrow_forwardWhen a sentence is entered, write the most relevant C code in that sentence. You should not write the following 3 rules without changing them. void maxinChar(char sentence[], size: in the given string and at most in the picture string) it is underdrawn. Within this function keep its escapes defense of a character in an array. By parameterizing this array to the findMinIndex and findMaxIndex functions, the set value is returned for the minimum and transition elements in this array. In the sentence given according to the returned values, you talk less and mostly about yourself. int findIndex[] is the smallest element in a given array of integers. The array int findx(Index] overnights inside the largest element in an array of integers given you. -------- Meaning: Enter a sentence: Hello world, welcome to the java programming lesson Max Character: o Minimum Character: jarrow_forwardEach function inside the class A(n)_ is a virtual function. Provide your thoughts by filling in the blanks.arrow_forward
- In C Programming: Write a function inputAllCourses() which receives an array of course pointers and the array’s size, then allows the user to input all courses in the array by calling inputCourse()arrow_forwardIn C++, Define a “Invalidanalyze” function that accepts an array of “Course” objects. It will return the following information to the caller: - The number of courses with empty or blank description - The number of courses with invalid negative units - The number of courses with invalid day number of the week - The total number of units for all invalid courses in the array Show how this method is being called and return proper information.arrow_forwardDon't use vector array .using ifstream and ofstream. C++ programarrow_forward
- javascript only: This quarter, your team manager is awarding commission to anyone who closes a sale worth $15,000 or more! The commission amount will be 10% of the value of their largest sale. Anyone who does not make this large of a sale will receive the base commission of $500. Write a function `salesCommission(arr)` that takes an array of objects containing salesperson names and their largest sale, and returns the total amount of commission money that needs to be paid out to the team. Example: salespeople = [ { name: "Susan", largestSale: 3000 }, { name: "Brianna", largestSale: 20000 }, { name: "Dwayne", largestSale: 13000 }, { name: "Delilah", largestSale: 26000 }, { name: "Fernando", largestSale: 8000 }, ]; console.log(salesCommission(salespeople)); // 6100arrow_forwardC++ code please (5) Implement the FindText() function, which has two strings as parameters. The first parameter is the text to be found in the user provided sample text, and the second parameter is the user provided sample text. The function returns the number of instances a word or phrase is found in the string. In the PrintMenu() function, prompt the user for a word or phrase to be found and then call FindText() in the PrintMenu() function. Before the prompt, call cin.ignore() to allow the user to input a new string.Ex: Enter a word or phrase to be found: more "more" instances: 5arrow_forwardIn C programming: Write a function printAllCourses() which receives an array of course pointers and the array’s size, then prints all courses in the array by calling printCourseRow()arrow_forward
- PROGRAMMING LANGUAGE: C++ You need to store hiring date and date of birth for teachers, and for students store their admission date and date of birth. You need to create a Date class for this purpose. Create objects of Date class in Teacher and Student class to store respective dates. You need to write print function in Teacher and Student classes as well to print all information of Teachers and Students. You need to perform composition to implement this task. Create objects of Teacher and Student classes in main function and call print function for both objects. Print Date class here. Print updated Teacher class here. Print updated Student class here. Print main function here. Print Outputshere.arrow_forwardSee the attached photo for the instruction. Use python programming language and please put comments in your codes. Thank you! input.txt LadnoonefeelsreadyNoonefeelshedeservesitAndyouknowwhyBecausenoonedoesItisgracepureandsimpleWeareinherentlyunworthysimplybecausewearehumanandallhumanbeingsayeandelvesanddwarvesandalltheotherracesareflawedButtheLightlovesusanywayItlovesusforwhatwesometimescanrisetoinraremomentsItlovesusforwhatwecandotohelpothersAnditlovesusbecausewecanhelpitshareitsmessagebystrivingdailytobeworthyeventhoughweunderstandthatwecannotevertrulybecomesoSostandtheretodayasIdidfeelingthatyoucannotpossiblydeserveitoreverbeworthyandknowthatyouareinthesameplaceeverysinglepaladinhaseverstoodarrow_forwardfunction myCompose(f,g){// TODO: return (f o g);// that is, a function that returns f(g(x)) when invoked on x.}arrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,