1+ /*********************************************************************************
2+ * (Maximum consecutive increasingly ordered substring) Write a program that *
3+ * prompts the user to enter a string and displays the maximum consecutive *
4+ * increasingly ordered substring. Analyze the time complexity of your program. *
5+ *********************************************************************************/
6+ import java .util .*;
7+
8+ public class Exercise_22_01 {
9+ public static void main (String [] args ) {
10+ // Create a Scanner
11+ Scanner input = new Scanner (System .in );
12+ LinkedList <Character > max = new LinkedList <>();
13+ LinkedList <Character > list = new LinkedList <>();
14+
15+ // Prompt the user to enter a string
16+ System .out .print ("Enter a string: " );
17+ String string = input .nextLine ();
18+
19+ // Find the maximum consecutive increasingly ordered substring
20+ for (int i = 0 ; i < string .length (); i ++) { // single loop
21+ if (list .size () > 1 && string .charAt (i ) <= list .getLast () &&
22+ list .contains (string .charAt (i ))) {
23+ list .clear (); // Simple statement
24+ }
25+
26+ list .add (string .charAt (i )); // Simple statement
27+
28+ if (list .size () > max .size ()) { // Simple statement
29+ max .clear ();
30+ max .addAll (list );
31+ }
32+ }
33+
34+ // Display the maximum consecutive
35+ // increasingly ordered substring
36+ for (Character ch : max ) { // single loop
37+ System .out .print (ch ); // Simple statement
38+ }
39+ System .out .println ();
40+ }
41+
42+ /*********************************************************************************
43+ * Analyze the time complexity of your program: *
44+ * 1 single loop * 3 simple statements = 3; *
45+ * 1 single loop * 1 simple statement = 1; *
46+ * *
47+ * T(n) = O(n) Linear time; *
48+ *********************************************************************************/
49+ }
0 commit comments