Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
11th Edition
ISBN: 9780134671710
Author: Y. Daniel Liang
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 22, Problem 22.1PE

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.
  • 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.

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 Ο ( n 2 ) because the loop gets iterated two times consecutively to determine the length of the string. Therefore, the running time is Ο ( n 2 ) .

Sample Output:

Enter a string: abcabcdgabmnsxy

Maximum consecutive substring is abmnsxy

Expert Solution & Answer
Check Mark
Program Plan Intro

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.
  • 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.
Program Description Answer

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 Ο(n2) because the loop gets iterated two times consecutively to determine the length of the string. Therefore, the running time

is Ο(n2) .

Sample Output

Enter a string: abcabcdgabmnsxy

Maximum consecutive substring is abmnsxy

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
I cannot program smart home automation rules from my device using a computer or phone, and I would like to know how to properly connect devices such as switches and sensors together ? Cisco Packet Tracer 1. Smart Home Automation:o Connect a temperature sensor and a fan to a home gateway.o Configure the home gateway so that the fan is activated when the temperature exceedsa set threshold (e.g., 30°C).2. WiFi Network Configuration:o Set up a wireless LAN with a unique SSID.o Enable WPA2 encryption to secure the WiFi network.o Implement MAC address filtering to allow only specific clients to connect.3. WLC Configuration:o Deploy at least two wireless access points connected to a Wireless LAN Controller(WLC).o Configure the WLC to manage the APs, broadcast the configured SSID, and applyconsistent security settings across all APs.
using r language for integration theta = integral 0 to infinity (x^4)*e^(-x^2)/2 dx (1) use the density function of standard normal distribution N(0,1) f(x) = 1/sqrt(2pi) * e^(-x^2)/2 -infinity <x<infinity as importance function and obtain an estimate theta 1 for theta set m=100 for the estimate whatt is the estimate theta 1? (2)use the density function of gamma (r=5 λ=1/2)distribution f(x)=λ^r/Γ(r) x^(r-1)e^(-λx) x>=0 as importance function and obtain an estimate theta 2 for theta set m=1000 fir the estimate what is the estimate theta2? (3) use simulation (repeat 1000 times) to estimate the variance of the estimates theta1 and theta 2 which one has smaller variance?
using r language A continuous random variable X has density function f(x)=1/56(3x^2+4x^3+5x^4).0<=x<=2 (1) secify the density g of the random variable Y you find for the acceptance rejection method. (2) what is the value of c you choose to use for the acceptance rejection method (3) use the acceptance rejection method to generate a random sample of size 1000 from the distribution of X .graph the density histogram of the sample and compare it with the density function f(x)

Chapter 22 Solutions

Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
1.1 Arrays in Data Structure | Declaration, Initialization, Memory representation; Author: Jenny's lectures CS/IT NET&JRF;https://www.youtube.com/watch?v=AT14lCXuMKI;License: Standard YouTube License, CC-BY
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License