File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Filter options
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Original file line number Diff line number Diff line change
1
+ # Create an algorithm that prints the longest substring of s in which
2
+ # the letters occur in alphabetical order. For example, if
3
+ # s = 'azcbobobegghakl', then your program should print:
4
+ # Longest substring in alphabetical order is: beggh
5
+
6
+ # In the case of ties, print the first substring.
7
+ # For example, if s = 'abcbcd', then your program should print:
8
+ # Longest substring in alphabetical order is: abc
9
+
10
+
11
+ def longest_substr (s ):
12
+ count = 0
13
+ maxcount = 0
14
+ result = 0
15
+ for char in range (len (s ) - 1 ):
16
+ if (s [char ] <= s [char + 1 ]):
17
+ count += 1
18
+ if count > maxcount :
19
+ maxcount = count
20
+ result = char + 1
21
+ else :
22
+ count = 0
23
+ startposition = result - maxcount
24
+ return startposition , result
25
+
26
+
27
+ # parent string
28
+ s = 'azbeggaklbeggh'
29
+
30
+ # longest substring indexes
31
+ start , end = longest_substr (s )
32
+
33
+ print ('Longest substring in alphabetical order is:' ,
34
+ s [start :end + 1 ])
You can’t perform that action at this time.
0 commit comments