Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
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
Write a program to implement binary search algorithm on a dynamic array of objects of “patient” class.“patient” class contains attributes (privately defined) patient_id (int), pat_name (string) andpat_app_date (string). “patient” class should also contain member functions (publicly defined):constructor, input and output function. User will provide values for attributes for objects of “patient”class as input. Objects are required to be stored in array in ascending order on basis of patient_id. Binarysearch function should check for an object of “patient” class which will be input by user as a key valueon basis of patient_id attribute. If object is found its index location in array should display, otherwiseprogram should display a message that value is not found.
A) Write a program and define a class of Student. Student has these attributes :Age (an int)FirstName (a string)LastName (a string)Social Security Number (an int)Add the default constructor Add a second constructor that sets the attributes of the class Add the copy constructor that creates a copy of the student and override assignment operator.  B) Create a vector of 100 students. Use a for loop to fill the vector . C) Add a function to your program that sorts the vector using selection sort in ascending order . D) Add a function to your program that searches the vector using binary search to find the youngest student .
C++ Write a function named “getTotalHourAndPayout” that accepts an array ofPayStub object pointers and its size. It will return the total number of hours andpayout amount for all of the PayStub in the given array. For example, if we have two paystubs of 40 hours and $10 pay rate with 50 hoursand $10 pay rate, it will return 90 hours and $950.Please show you would call and test this function.

Chapter 22 Solutions

Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)

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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
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