Consecutive sequenced numbers in a string
Last Updated :
24 Apr, 2025
Given a string s that contains only numeric digits, we need to check whether that strings contains numbers in a consecutive sequential manner in increasing order. If the string contains a valid sequence of consecutive integers, print "Yes"
followed by the starting number of the sequence. Otherwise, print "No"
.
Note: Negative numbers are not considered part of this problem. So we consider that input only contains non negative integer.
Examples:
Input : s = "1234"
Output : "Yes", "1"
Explanation : There are 1, 2, 3, 4 which are consecutive and in increasing order and the starting number is 1
Input : s = "91012"
Output : "No"
Explanation : There are no such sequence in the string.
Input : s = "99100"
Output : "Yes", "99"
Explanation : The consecutive sequential
numbers are 99, 100
Input : s = "010203"
Output : "No"
Explanation : Although at first glance there seems to be 01, 02, 03. But those wouldn't be considered a number. 01 is not 1 it's 0, 1
The idea is to try all lengths starting from one. We start taking length 1 or one character at first (assuming that our string starts with 1 digit number) and then form a new string by concatenating the next number until the length of new string is equal to original string. If length 1 does not give us result, then we try length 2 and so on until, either we get the desired sequence or we have tried till string-ength/2
Let's take string "99100"

C++
#include <iostream>
using namespace std;
int isSeq(string &s)
{
int n = s.size();
// Try all possible lengths for the first number
for (int len = 1; len <= n / 2; len++) {
// new string containing the starting
// substring of input string
string sub = s.substr(0, len);
// If the first number has leading zeros and is not "0", skip
if (sub.length() > 1 && sub[0] == '0') {
continue;
}
// Convert the substring to integer
int num = stoi(sub);
int start = num;
// while loop until the new_string is
// smaller than input string
string seq = sub;
while (seq.size() < n) {
num++;
seq += to_string(num);
}
if (seq == s) {
return start;
}
}
// If no valid sequence is found, return -1
return -1;
}
int main()
{
string s = "99100";
int start = isSeq(s);
if (start != -1)
cout << "Yes, " << start << endl;
else
cout << "No" << endl;
return 0;
}
Java
import java.util.Scanner;
public class GfG{
public static int isSeq(String s) {
int n = s.length();
// Try all possible lengths for the first number
for (int len = 1; len <= n / 2; len++) {
// new string containing the starting substring of input string
String sub = s.substring(0, len);
// If the first number has leading zeros and is not "0", skip
if (sub.length() > 1 && sub.charAt(0) == '0') {
continue;
}
// Convert the substring to integer
int num = Integer.parseInt(sub);
int start = num;
// while loop until the new_string is smaller than input string
String seq = sub;
while (seq.length() < n) {
num++;
seq += Integer.toString(num);
}
if (seq.equals(s)) {
return start;
}
}
// If no valid sequence is found, return -1
return -1;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = "99100";
int start = isSeq(s);
if (start != -1)
System.out.println("Yes, " + start);
else
System.out.println("No");
scanner.close();
}
}
Python
def is_seq(s):
n = len(s)
# Try all possible lengths for the first number
for length in range(1, n // 2 + 1):
# new string containing the starting substring of input string
sub = s[:length]
# If the first number has leading zeros and is not "0", skip
if len(sub) > 1 and sub[0] == '0':
continue
# Convert the substring to integer
num = int(sub)
start = num
# while loop until the new_string is smaller than input string
seq = sub
while len(seq) < n:
num += 1
seq += str(num)
if seq == s:
return start
# If no valid sequence is found, return -1
return -1
s = "99100"
start = is_seq(s)
if start != -1:
print(f'Yes, {start}')
else:
print('No')
C#
using System;
class GfG{
public static int IsSeq(string s) {
int n = s.Length;
// Try all possible lengths for the first number
for (int len = 1; len <= n / 2; len++) {
// new string containing the starting substring of input string
string sub = s.Substring(0, len);
// If the first number has leading zeros and is not "0", skip
if (sub.Length > 1 && sub[0] == '0') {
continue;
}
// Convert the substring to integer
int num = int.Parse(sub);
int start = num;
// while loop until the new_string is smaller than input string
string seq = sub;
while (seq.Length < n) {
num++;
seq += num.ToString();
}
if (seq == s) {
return start;
}
}
// If no valid sequence is found, return -1
return -1;
}
static void Main() {
string s = "99100";
int start = IsSeq(s);
if (start != -1)
Console.WriteLine("Yes, " + start);
else
Console.WriteLine("No");
}
}
JavaScript
function isSeq(s) {
const n = s.length;
// Try all possible lengths for the first number
for (let len = 1; len <= Math.floor(n / 2); len++) {
// new string containing the starting substring of input string
const sub = s.substring(0, len);
// If the first number has leading zeros and is not "0", skip
if (sub.length > 1 && sub[0] === '0') {
continue;
}
// Convert the substring to integer
let num = parseInt(sub);
const start = num;
// while loop until the new_string is smaller than input string
let seq = sub;
while (seq.length < n) {
num++;
seq += num.toString();
}
if (seq === s) {
return start;
}
}
// If no valid sequence is found, return -1
return -1;
}
const s = "99100";
const start = isSeq(s);
if (start !== -1)
console.log(`Yes, ${start}`);
else
console.log("No");
Time Complexity: O(n2), n is the size of the given string.
Auxiliary Space: O(n)
Similar Reads
Maximum consecutive numbers present in an array Find the length of maximum number of consecutive numbers jumbled up in an array.Examples: Input : arr[] = {1, 94, 93, 1000, 5, 92, 78};Output : 3 The largest set of consecutive elements is92, 93, 94 Input : arr[] = {1, 5, 92, 4, 78, 6, 7};Output : 4 The largest set of consecutive elements is4, 5, 6,
15+ min read
Calculate sum of all numbers present in a string Given a string S containing alphanumeric characters, The task is to calculate the sum of all numbers present in the string. Examples: Input: 1abc23Output: 24Explanation: 1 + 23 = 24 Input: geeks4geeksOutput: 4 Input: 1abc2x30yz67Output: 100 Recommended PracticeSum of numbers in stringTry It!Approach
13 min read
Count number of binary strings without consecutive 1âs : Set 2 Given a positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1âs. Examples: Input: N = 5 Output: 5 Explanation: The non-negative integers <= 5 with their corresponding binary representations are: 0 : 0 1 : 1 2 : 10 3 : 11 4 :
15+ min read
Sum of numbers formed by consecutive digits present in a given string Given a string S consisting of digits [0 - 9] and lowercase alphabets, the task is to calculate the sum of all numbers represented by continuous sequences of digits present in the string S. Examples: Input: S = "11aa32bbb5"Output: 48Explanation: The consecutive sequence of numbers present in the str
5 min read
Kth space-separated number from a given string Given a string S consisting of space-separated integers, the task is to extract the Kth number present in the string. Note: The string contains at least K numbers in it. Examples: Input: S = "12 13 15", K= 3Output: 15Explanation: The 3rd integer in the above string is 15. Input: S = "10 20 30 40", K
9 min read