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)
- Q.2. Architecture performance [25 marks] Consider two different implementations, M1 and M2, of the same instruction set. M1 has a clock rate of 2 GHz and M2 has a clock rate of 3.3 GHz. There are two classes of instructions with the following CPIs: Class A CPI for M1 CPI for M2 2.f 1.g B 5 3 C 6 4 Note that the dots in 2 fand 1.g indicate decimal points and not multiplication. a) What are the peak MIPS performances for both machines? b) Which implementation is faster, if half the instructions executed in a certain program are from class A, while the rest are divided equally among classes B and C. c) What speedup factor for the execution of class-A instructions would lead to 20% overall speedup? d) What is the maximum possible speedup that can be achieved by only improving the execution of class-A instructions? Explain why. e) What is the clock rate required for microprocessor M1 to be a "1000 MIPS" (not peak MIPS) processor?arrow_forwardPLEASE SOLVE STEP BY STEP WITHOUT ARTIFICIAL INTELLIGENCE OR CHATGPT I don't understand why you use chatgpt, if I wanted to I would do it myself, I need to learn from you, not from being a d amn robot. SOLVE STEP BY STEP I WANT THE DIAGRAM PERFECTLY IN SIMULINKarrow_forwardI need to develop and run a program that prompts the user to enter a positive integer n, and then calculate the value of n factorial n! = multiplication of all integers between 1 and n, and print the value n! on the screen. This is for C*.arrow_forward
- I need to develop and run a C* program to sum up integers from 1 to 100, and print out the sum value on the screen. Can someone help please?arrow_forwardGiven the schema below for the widgetshop, provide a schema diagram. Schema name Attributes Widget-schema Customer-schema (stocknum, manufacturer, description, weight, price, inventory) (custnum, name, address) Purchased-schema (custnum, stocknum, pdate) Requestedby-schema (stocknum, custnum) Newitem-schema (stocknum, manufacturer, description) Employee-schema (ssn, name, address, salary) You can remove the Newitem-schema (red).arrow_forwardTrue or False: Given the sets F and G with F being an element of G, is it always ture that P(F) is an element of P(G)? (P(F) and P(G) mean power sets). Why?arrow_forward
- Can you please simplify (the domain is not empty) ∃xF (x) → ¬∃x(F (x) ∨ ¬G(x)). Foarrow_forwardHistogramUse par(mfrow=c(2,2)) and output 4 plots with different argument settings.arrow_forward(use R language)Scatter plot(a). Run the R code example, and look at the help file for plot() function. Try different values for arguments:type, pch, lty, lwd, col(b). Use par(mfrow=c(3,2)) and output 6 plots with different argument settings.arrow_forward
- 1. Draw flow charts for each of the following;a) A system that reads three numbers and prints the value of the largest number.b) A system reads an employee name (NAME), overtime hours worked (OVERTIME), hours absent(ABSENT) and determines the bonus payment (PAYMENT).arrow_forwardScenario You work for a small company that exports artisan chocolate. Although you measure your products in kilograms, you often get orders in both pounds and ounces. You have decided that rather than have to look up conversions all the time, you could use Python code to take inputs to make conversions between the different units of measurement. You will write three blocks of code. The first will convert kilograms to pounds and ounces. The second will convert pounds to kilograms and ounces. The third will convert ounces to kilograms and pounds. The conversions are as follows: 1 kilogram = 35.274 ounces 1 kilogram = 2.20462 pounds 1 pound = 0.453592 kilograms 1 pound = 16 ounces 1 ounce = 0.0283 kilograms 1 ounce = 0.0625 pounds For the purposes of this activity the template for a function has been provided. You have not yet covered functions in the course, but they are a way of reusing code. Like a Python script, a function can have zero or more parameters. In the code window you…arrow_forwardmake a screen capture showing the StegExpose resultsarrow_forward
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
- Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningEBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT