Find longest length number in a string
Given a string of digits and characters. Write a program to find the number with the maximum number of digits in a string.
Note: The number may not be the greatest number in the string. For example, if the string is “a123bc321” then the answer can be 123 or 321 as the problem is to find the number with the longest length and not the largest value.
Examples:
Input: geeks100for1234geeks
Output: 1234
Input: abx12321bst1234yz
Output: 12321
Approach: The idea is to traverse the string and if a digit is encountered, store its position and from that position traverse further until a character occurs. Every time a continuous series of a digit is encountered, store its length and match it with the length previously find series of a digit to find out the maximum of all the continuous series of digits.
Below is the implementation of above approach.
C++
#include <iostream>
using namespace std;
string longestInteger(string str, int l)
{
int count = 0, max = 0, pos = -1, pre_pos, pre_len, len = 0;
for ( int i = 0; i < l; i++) {
pre_pos = pos;
pre_len = len;
count = 0;
len = 0;
if ( isdigit (str[i]))
pos = i;
while ( isdigit (str[i])) {
count++;
i++;
len++;
}
if (count > max) {
max = count;
}
else {
pos = pre_pos;
len = pre_len;
}
}
return (str.substr(pos, len));
}
int main()
{
string str = "geeks100for1234geeks" ;
int l = str.length();
cout << longestInteger(str, l);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GFG
{
static String longestInteger(String str, int l)
{
int count = 0 , max = 0 ,
pos = - 1 , pre_pos,
pre_len, len = 0 ;
for ( int i = 0 ; i < l; i++)
{
pre_pos = pos;
pre_len = len;
count = 0 ;
len = 0 ;
if (Character.isDigit(str.charAt(i)))
pos = i;
while (Character.isDigit(str.charAt(i)))
{
count++;
i++;
len++;
}
if (count > max)
{
max = count;
}
else
{
pos = pre_pos;
len = pre_len;
}
}
return (str.substring(pos, pos + len));
}
public static void main(String[] args)
{
String str = "geeks100for1234geeks" ;
int l = str.length();
System.out.print(longestInteger(str, l));
}
}
|
C#
using System;
class GFG
{
static string longestInteger( string str,
int l)
{
int count = 0, max = 0,
pos = -1, pre_pos,
pre_len, len = 0;
for ( int i = 0; i < l; i++)
{
pre_pos = pos;
pre_len = len;
count = 0;
len = 0;
if (Char.IsDigit(str[i]))
pos = i;
while (Char.IsDigit(str[i]))
{
count++;
i++;
len++;
}
if (count > max)
{
max = count;
}
else
{
pos = pre_pos;
len = pre_len;
}
}
return (str.Substring(pos, len));
}
public static void Main()
{
string str = "geeks100for1234geeks" ;
int l = str.Length;
Console.Write(longestInteger(str, l));
}
}
|
Python 3
def longestInteger(s, length):
count = 0
maximum = 0
pos = - 1
l = 0
for i in range (length):
pre_pos = pos
pre_len = l
count = 0
l = 0
if (s[i].isdecimal()):
pos = i
while (s[i].isdecimal()):
count + = 1
i + = 1
l + = 1
if (count > maximum):
maximum = count
else :
pos = pre_pos
l = pre_len
return (s[pos: pos + l])
s = "geeks100for1234geeks"
length = len (s)
print (longestInteger(s, length))
|
PHP
<?php
function longestInteger( $str , $l )
{
$count = 0;
$max = 0;
$pos = -1;
$pre_pos = 0;
$pre_len = 0;
$len = 0;
for ( $i = 0; $i < $l ; $i ++)
{
$pre_pos = $pos ;
$pre_len = $len ;
$count = 0;
$len = 0;
if ( is_numeric ( $str [ $i ]))
$pos = $i ;
while ( is_numeric ( $str [ $i ]))
{
$count ++;
$i ++;
$len ++;
}
if ( $count > $max )
{
$max = $count ;
}
else
{
$pos = $pre_pos ;
$len = $pre_len ;
}
}
return ( substr ( $str , $pos , $len ));
}
$str = "geeks100for1234geeks" ;
$l = strlen ( $str );
echo longestInteger( $str , $l );
?>
|
Javascript
<script>
function longestInteger(str,l)
{
let count = 0, max = 0,
pos = -1, pre_pos,
pre_len, len = 0;
for (let i = 0; i < l; i++)
{
pre_pos = pos;
pre_len = len;
count = 0;
len = 0;
if (!isNaN(String(str[i]) * 1))
pos = i;
while (!isNaN(String(str[i]) * 1))
{
count++;
i++;
len++;
}
if (count > max)
{
max = count;
}
else
{
pos = pre_pos;
len = pre_len;
}
}
return (str.substring(pos, pos + len));
}
let str = "geeks100for1234geeks" ;
let l = str.length;
document.write(longestInteger(str, l));
</script>
|
Time Complexity: O(n)
Auxiliary Space Complexity: O(1)
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...