6 kyu

Longest Alternating Substring

Description:

Task

Given a string of digits, return the longest substring with alternating odd/even or even/odd digits. If two or more substrings have the same length, return the substring that occurs first.

Examples

longest_substring("225424272163254474441338664823")  "272163254"
# substrings = 254, 272163254, 474, 41, 38, 23

longest_substring("594127169973391692147228678476")  "16921472"
# substrings = 94127, 169, 16921472, 678, 476

longest_substring("721449827599186159274227324466")  "7214"
# substrings = 7214, 498, 27, 18, 61, 9274, 27, 32
# 7214 and 9274 have same length, but 7214 occurs first.

longest_substring("20")  "2"

longest_substring("")  ""

Input Constraints

  • 0 <= len(digits) <= 10^5
Algorithms
Logic
Regular Expressions
Strings

More By Author:

Check out these other kata created by saudiGuy

Stats:

CreatedAug 15, 2023
PublishedAug 15, 2023
Warriors Trained259
Total Skips9
Total Code Submissions754
Total Times Completed144
Python Completions144
Total Stars7
% of votes with a positive feedback rating95% of 41
Total "Very Satisfied" Votes37
Total "Somewhat Satisfied" Votes4
Total "Not Satisfied" Votes0
Total Rank Assessments21
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • saudiGuy Avatar
  • akar-0 Avatar
  • dfhwze Avatar
Ad