Consecutive sequenced numbers in a string
Given a string that contains only numeric digits, we need to check whether that strings contains numbers in a consecutive sequential manner in increasing order.
Note: Negative numbers are not considered part of this problem. So we consider that input only contains positive integer.
Examples:
Input : str = "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 : str = "91012"
Output : No
Explanation :
There are no such sequence in the
string.
Input : str = "99100"
Output : Yes
99
Explanation : The consecutive sequential
numbers are 99, 100
Input : str = "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
Approach: An easily implementable and useful approach is to start taking 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.
Perhaps an example may clarify :
Let’s take string “99100”
Below is the implementation:
C++
#include <iostream>
using namespace std;
int isConsecutive(string str)
{
int start;
int length = str.size();
for ( int i = 0; i < length / 2; i++) {
string new_str = str.substr(0, i + 1);
int num = atoi (new_str.c_str());
start = num;
while (new_str.size() < length) {
num++;
new_str += to_string(num);
}
if (new_str == str)
return start;
}
return -1;
}
int main()
{
string str = "99100" ;
cout << "String: " << str << endl;
int start = isConsecutive(str);
if (start != -1)
cout << "Yes \n" << start << endl;
else
cout << "No" << endl;
string str1 = "121315" ;
cout << "\nString: " << str1 << endl;
start = isConsecutive(str1);
if (start != -1)
cout << "Yes \n" << start << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
class GFG
{
static int isConsecutive(String str)
{
int start;
int length = str.length();
for ( int i = 0 ; i < length / 2 ; i++)
{
String new_str = str.substring( 0 , i + 1 );
int num = Integer.parseInt(new_str);
start = num;
while (new_str.length() < length)
{
num++;
new_str = new_str + String.valueOf(num);
}
if (new_str.equals(str))
return start;
}
return - 1 ;
}
public static void main(String[] args)
{
String str = "99100" ;
System.out.println( "String: " + str);
int start = isConsecutive(str);
if (start != - 1 )
System.out.println( "Yes \n" + start);
else
System.out.println( "No" );
String str1 = "121315" ;
System.out.println( "\nString: " + str1);
start = isConsecutive(str1);
if (start != - 1 )
System.out.println( "Yes \n" + start);
else
System.out.println( "No" );
}
}
|
Python3
def isConsecutive(strs):
start = 0 ;
length = len (strs);
for i in range (length / / 2 ):
new_str = strs[ 0 : i + 1 ];
num = int (new_str);
start = num;
while ( len (new_str) < length):
num + = 1 ;
new_str = new_str + str (num);
if (new_str = = (strs)):
return start;
return - 1 ;
if __name__ = = '__main__' :
str0 = "99100" ;
print ( "String: " + str0);
start = isConsecutive(str0);
if (start ! = - 1 ):
print ( "Yes \n" , start);
else :
print ( "No" );
str1 = "121315" ;
print ( "\nString: " , str1);
start = isConsecutive(str1);
if (start ! = - 1 ):
print ( "Yes \n" , start);
else :
print ( "No" );
|
C#
using System;
class GFG
{
static int isConsecutive(String str)
{
int start;
int length = str.Length;
for ( int i = 0; i < length / 2; i++)
{
String new_str = str.Substring(0, i + 1);
int num = int .Parse(new_str);
start = num;
while (new_str.Length < length)
{
num++;
new_str = new_str + String.Join( "" ,num);
}
if (new_str.Equals(str))
return start;
}
return -1;
}
public static void Main(String[] args)
{
String str = "99100" ;
Console.WriteLine( "String: " + str);
int start = isConsecutive(str);
if (start != -1)
Console.WriteLine( "Yes \n" + start);
else
Console.WriteLine( "No" );
String str1 = "121315" ;
Console.WriteLine( "\nString: " + str1);
start = isConsecutive(str1);
if (start != -1)
Console.WriteLine( "Yes \n" + start);
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function isConsecutive(str)
{
let start;
let length = str.length;
for (let i = 0; i < length / 2; i++)
{
let new_str = str.substring(0, i + 1);
let num = parseInt(new_str);
start = num;
while (new_str.length < length)
{
num++;
new_str = new_str + (num).toString();
}
if (new_str == (str))
return start;
}
return -1;
}
let str = "99100" ;
document.write( "String: " + str+ "<br>" );
let start = isConsecutive(str);
if (start != -1)
document.write( "Yes <br>" + start+ "<br>" );
else
document.write( "No<br>" );
let str1 = "121315" ;
document.write( "<br>String: " + str1+ "<br>" );
start = isConsecutive(str1);
if (start != -1)
document.write( "Yes <br>" + start+ "<br>" );
else
document.write( "No<br>" );
</script>
|
Output
String: 99100
Yes
99
String: 121315
No
Time Complexity: O(n2), where n is the size of the given string.
Auxiliary Space: O(n), where n is the size of the given string.
Last Updated :
09 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...