Recursion: Consecutive Number Series

Published by Deep Xavier in

Write a function that will return true if a given string (divided and grouped into a size) will contain a set of consecutive numbers (regardless of orientation: whether ascending or descending), otherwise, return false.

Examples

isConsecutive("121314151617") ➞ true
// Contains a set of consecutive ascending numbers
// if grouped into 2's : 12, 13, 14, 15, 16, 17

isConsecutive("123124125") ➞ true
// Contains a set of consecutive ascending numbers
// if grouped into 3's : 123, 124, 125

isConsecutive("32332432536") ➞ false
// Regardless of the grouping size, the numbers can't be consecutive.

isConsecutive("326325324323") ➞ true
// Contains a set of consecutive descending numbers
// if grouped into 3's : 326, 325, 324, 323

isConsecutive("667666") ➞ true
// Consecutive descending numbers: 667 and 666.

isConsecutive("999897959493") ➞ false

IMPORTANT

The expected solution for this challenge is done recursively. Please check out the Resources tab for more details about recursion in Java.

Notes

  • A number can consist of any number of digits, so long as the numbers are adjacent to each other, and the string has at least two of them.
  • An iterative version of this challenge can be found via this link.
  • A collection of challenges in recursion can be found via this link.
Watch a quick demo on how Edabit works.