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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for finding the longest
// length integer
#include <iostream>
using namespace std;
  
string longestInteger(string str, int l)
{
    int count = 0, max = 0, pos = -1, pre_pos, pre_len, len = 0;
    // Traverse the string
    for (int i = 0; i < l; i++) {
        // Store the previous position and previous length
        // of the digits encountered.
        pre_pos = pos;
        pre_len = len;
        count = 0;
        len = 0;
  
        // If first digit occurs, store its position in pos
        if (isdigit(str[i]))
            pos = i;
  
        // Traverse the string till a character occurs.
        while (isdigit(str[i])) {
            count++;
            i++;
            len++;
        }
  
        // Check if the length of the string is
        // greater than the previous ones or not.
        if (count > max) {
            max = count;
        }
        else {
            pos = pre_pos;
            len = pre_len;
        }
    }
    return (str.substr(pos, len));
}
  
// Driver code
int main()
{
    string str = "geeks100for1234geeks";
    int l = str.length();
    cout << longestInteger(str, l);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for finding the 
// longest length integer
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;
      
    // Traverse the string
    for (int i = 0; i < l; i++) 
    {
        // Store the previous position
        // and previous length of the
        // digits encountered.
        pre_pos = pos;
        pre_len = len;
        count = 0;
        len = 0;
  
        // If first digit occurs,
        // store its position in pos
        if (Character.isDigit(str.charAt(i)))
            pos = i;
  
        // Traverse the string 
        // till a character occurs.
        while (Character.isDigit(str.charAt(i))) 
        {
            count++;
            i++;
            len++;
        }
  
        // Check if the length of 
        // the string is greater
        // than the previous ones 
        // or not.
        if (count > max)
        {
            max = count;
        }
        else
        {
            pos = pre_pos;
            len = pre_len;
        }
    }
    return (str.substring(pos, pos + len));
}
  
// Driver code
public static void main(String[] args)
{
    String str = "geeks100for1234geeks";
    int l = str.length();
    System.out.print(longestInteger(str, l));
}
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code for finding the 
// longest length integer
using System;
  
class GFG
{
static string longestInteger(string str, 
                             int l)
{
    int count = 0, max = 0, 
        pos = -1, pre_pos, 
        pre_len, len = 0;
      
    // Traverse the string
    for (int i = 0; i < l; i++) 
    {
        // Store the previous position
        // and previous length of the
        // digits encountered.
        pre_pos = pos;
        pre_len = len;
        count = 0;
        len = 0;
  
        // If first digit occurs,
        // store its position in pos
        if (Char.IsDigit(str[i]))
            pos = i;
  
        // Traverse the string 
        // till a character occurs.
        while (Char.IsDigit(str[i])) 
        {
            count++;
            i++;
            len++;
        }
  
        // Check if the length of 
        // the string is greater
        // than the previous ones 
        // or not.
        if (count > max)
        {
            max = count;
        }
        else
        {
            pos = pre_pos;
            len = pre_len;
        }
    }
    return (str.Substring(pos, len));
}
  
// Driver code
public static void Main()
{
    string str = "geeks100for1234geeks";
    int l = str.Length;
    Console.Write(longestInteger(str, l));
}
}
  
// This code is contributed 
// by ChitraNayal

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code for finding the 
# longest length integer
  
def longestInteger(s, length):
    count = 0
    maximum = 0
    pos = -1
    l = 0
      
    # Traverse the string
    for i in range(length):
          
        # Store the previous position
        # and previous length of 
        # the digits encountered.
        pre_pos = pos
        pre_len = l
        count = 0
        l = 0
  
        # If first digit occurs,
        # store its position in pos
        if (s[i].isdecimal()):
            pos = i
  
        # Traverse the string 
        # till a character occurs.
        while (s[i].isdecimal()):
            count += 1
            i += 1
            l += 1
  
        # Check if the length of 
        # the string is greater 
        # than the previous ones
        # or not.
        if (count > maximum):
            maximum = count
          
        else:
            pos = pre_pos
            l = pre_len
  
    return (s[pos: pos + l])
  
# Driver code
s = "geeks100for1234geeks"
length = len(s)
print(longestInteger(s, length))
  
# This code is contributed 
# by ChitraNayal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code for finding the 
// longest length integer
  
function longestInteger($str, $l)
{
    $count = 0;
    $max = 0;
    $pos = -1;
    $pre_pos = 0;
    $pre_len = 0;
    $len = 0;
      
    // Traverse the string
    for ($i = 0; $i < $l; $i++) 
    {
        // Store the previous position
        // and previous length of 
        // the digits encountered.
        $pre_pos = $pos;
        $pre_len = $len;
        $count = 0;
        $len = 0;
  
        // If first digit occurs,
        // store its position in pos
        if (is_numeric($str[$i]))
            $pos = $i;
  
        // Traverse the string till
        // a character occurs.
        while (is_numeric($str[$i]))
        {
            $count++;
            $i++;
            $len++;
        }
  
        // Check if the length of 
        // the string is greater
        // than the previous ones
        // or not.
        if ($count > $max)
        {
            $max = $count;
        }
        else 
        {
            $pos = $pre_pos;
            $len = $pre_len;
        }
    }
    return (substr($str, $pos, $len));
}
  
// Driver code
$str = "geeks100for1234geeks";
$l = strlen($str);
echo longestInteger($str, $l);
  
// This code is contributed
// by ChitraNayal
?>

chevron_right


Output:

1234

Time Complexity: O(n)
Auxiliary Space Complexity: O(1)



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Ita_c, ManasChhabra2