Consecutive sequenced numbers in a string (original) (raw)

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 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)