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
What are the major threats of using the internet? How do you use it? How do children use it? How canwe secure it? Provide four references with your answer. Two of the refernces can be from an article and the other two from websites.
Assume that a string of name & surname is saved in S. The alphabetical characters in S can be in lowercase and/or uppercase letters. Name and surname are assumed to be separated by a space character and the string ends with a full stop "." character. Write an assembly language program that will copy the name to NAME in lowercase and the surname to SNAME in uppercase letters. Assume that name and/or surname cannot exceed 20 characters. The program should be general and work with every possible string with name & surname. However, you can consider the data segment definition given below in your program. .DATA S DB 'Mahmoud Obaid." NAME DB 20 DUP(?) SNAME DB 20 DUP(?) Hint: Uppercase characters are ordered between 'A' (41H) and 'Z' (5AH) and lowercase characters are ordered between 'a' (61H) and 'z' (7AH) in the in the ASCII Code table. For lowercase letters, bit 5 (d5) of the ASCII code is 1 where for uppercase letters it is 0. For example, Letter 'h' Binary ASCII 01101000 68H 'H'…
What did you find most interesting or surprising about the scientist Lavoiser?

Chapter 22 Solutions

Instructor Solutions Manual For Introduction To Java Programming And Data Structures, Comprehensive Version, 11th 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