Given an alphanumeric string, extract maximum numeric value from that string. Alphabets will only be in lower case.
One approach to solving the problem is discussed here, other using Regular expressions is given in Set 2
Examples:
Input : 100klh564abc365bg
Output : 564
Maximum numeric value among 100, 564
and 365 is 564.
Input : abchsd0sdhs
Output : 0
Its solution is simple i.e. Start traversing the string and perform two operations:
- 1) If a numeric value is present at the current index then convert it into an integer
num = num*10 + (str[i]-'0')
- 2) Otherwise, update the maximum value and reset num = 0.
Return the maximum value at the last.
C++
#include<bits/stdc++.h>
using namespace std;
int extractMaximum(string str)
{
int num = 0, res = 0;
for ( int i = 0; i<str.length(); i++)
{
if (str[i] >= '0' && str[i] <= '9' )
num = num * 10 + (str[i]- '0' );
else
{
res = max(res, num);
num = 0;
}
}
return max(res, num);
}
int main()
{
string str = "100klh564abc365bg" ;
cout << extractMaximum(str);
return 0;
}
|
Java
class GFG
{
static int extractMaximum(String str)
{
int num = 0 , res = 0 ;
for ( int i = 0 ; i<str.length(); i++)
{
if (Character.isDigit(str.charAt(i)))
num = num * 10 + (str.charAt(i)- '0' );
else
{
res = Math.max(res, num);
num = 0 ;
}
}
return Math.max(res, num);
}
public static void main(String[] args)
{
String str = "100klh564abc365bg" ;
System.out.println(extractMaximum(str));
}
}
|
Python3
def extractMaximum(ss):
num, res = 0 , 0
for i in range ( len (ss)):
if ss[i] > = "0" and ss[i] < = "9" :
num = num * 10 + int ( int (ss[i]) - 0 )
else :
res = max (res, num)
num = 0
return max (res, num)
ss = "100klh564abc365bg"
print (extractMaximum(ss))
|
C#
using System;
class GFG {
static int extractMaximum(String str)
{
int num = 0, res = 0;
for ( int i = 0; i < str.Length; i++)
{
if ( char .IsDigit(str[i]))
num = num * 10 + (str[i]- '0' );
else
{
res = Math.Max(res, num);
num = 0;
}
}
return Math.Max(res, num);
}
public static void Main()
{
String str = "100klh564abc365bg" ;
Console.Write(extractMaximum(str));
}
}
|
PHP
<?php
function extractMaximum( $str )
{
$num = 0;
$res = 0;
for ( $i = 0; $i < strlen ( $str ); $i ++)
{
if ( $str [ $i ] >= '0' && $str [ $i ] <= '9' )
$num = $num * 10 + ( $str [ $i ]- '0' );
else
{
$res = max( $res , $num );
$num = 0;
}
}
return max( $res , $num );
}
$str = "100klh564abc365bg" ;
echo extractMaximum( $str );
?>
|
Javascript
<script>
function extractMaximum(str)
{
var num = 0, res = 0;
for (i = 0; i<str.length; i++)
{
if (isDigit(str.charAt(i)))
num = num * 10 +
(str.charAt(i).charCodeAt(0)-
'0' .charCodeAt(0));
else
{
res = Math.max(res, num);
num = 0;
}
}
return Math.max(res, num);
}
function isDigit(c) {
return c >= '0' && c <= '9' ;
}
var str = "100klh564abc365bg" ;
document.write(extractMaximum(str));
</script>
|
Time Complexity: O(n), where n is the size of the given string str
Auxiliary Space: O(1)
But in the case of large numbers above program wouldn’t work because of the integer range in C and C++. So, to handle the case of large numbers we have to take each numeric value as a separate string and then find the maximum value.
1) Start traversing the given string.
Continue traversing if there are any
leading zeroes or any lowercase character.
b) Form a string of integer values.
c) Update the maximum string.
i) If the maximum string and current
string are having equal lengths then
on the basis of the first unmatched
value return maximum string.
ii) If both are having different lengths
then return the string with greater
length.
2) Return maximum string.
C++
#include<bits/stdc++.h>
using namespace std;
string maximumNum(string curr_num, string res)
{
int len1 = curr_num.length();
int len2 = res.length();
if (len1 == len2)
{
int i = 0;
while (curr_num[i]== res[i])
i++;
if (curr_num[i] < res[i])
return res;
else
return curr_num;
}
return len1 < len2 ? res: curr_num;
}
string extractMaximum(string str)
{
int n = str.length();
string curr_num = "" ;
string res;
for ( int i = 0; i<n; i++)
{
while (i<n && str[i]== '0' )
i++;
while (i<n && str[i]>= '0' && str[i]<= '9' )
{
curr_num = curr_num + str[i];
i++;
}
if (i == n)
break ;
if (curr_num.size() > 0)
i--;
res = maximumNum(curr_num, res);
curr_num = "" ;
}
if (curr_num.size()== 0 && res.size()== 0)
res = res + '0' ;
return maximumNum(curr_num, res);
}
int main()
{
string str = "100klh564abc365bg" ;
cout << extractMaximum(str) << endl;
return 0;
}
|
Java
class GFG
{
static String maximumNum(String curr_num, String res)
{
int len1 = curr_num.length();
int len2 = res.length();
if (len1 == len2)
{
int i = 0 ;
while (curr_num.charAt(i) == res.charAt(i))
i++;
if (curr_num.charAt(i) < res.charAt(i))
return res;
else
return curr_num;
}
return len1 < len2 ? res: curr_num;
}
static String extractMaximum(String str)
{
int n = str.length();
String curr_num = "" ;
String res= "" ;
for ( int i = 0 ; i<n; i++)
{
while (i<n && str.charAt(i)== '0' )
i++;
while (i<n && Character.isDigit(str.charAt(i)))
{
curr_num = curr_num + str.charAt(i);
i++;
}
if (i == n)
break ;
if (curr_num.length() > 0 )
i--;
res = maximumNum(curr_num, res);
curr_num = "" ;
}
if (curr_num.length() == 0 && res.length() == 0 )
res = res + '0' ;
return maximumNum(curr_num, res);
}
public static void main(String[] args)
{
String str = "100klh564abc365bg" ;
System.out.println(extractMaximum(str));
}
}
|
Python3
def maximumNum(curr_num, res):
len1 = len (curr_num);
len2 = len (res);
if (len1 = = len2):
i = 0 ;
while (curr_num[i] = = res[i]):
i + = 1 ;
if (curr_num[i] < res[i]):
return res;
else :
return curr_num;
return res if (len1 < len2) else curr_num;
def extractMaximum( str ):
n = len ( str );
curr_num = "";
res = "";
for i in range (n):
while (i < n and str [i] = = '0' ):
i + = 1 ;
while (i < n and str [i] > = '0' and
str [i] < = '9' ):
curr_num + = str [i];
i + = 1 ;
if (i = = n):
break ;
if ( len (curr_num) > 0 ):
i - = 1 ;
res = maximumNum(curr_num, res);
curr_num = "";
if ( len (curr_num) = = 0 and len (res) = = 0 ):
res + = '0' ;
return maximumNum(curr_num, res);
str = "100klh564abc365bg" ;
print (extractMaximum( str ));
|
C#
using System;
class GFG
{
static String maximumNum( string curr_num, string res)
{
int len1 = curr_num.Length;
int len2 = res.Length;
if (len1 == len2)
{
int i = 0;
while (curr_num[i] == res[i])
i++;
if (curr_num[i] < res[i])
return res;
else
return curr_num;
}
return len1 < len2 ? res: curr_num;
}
static string extractMaximum( string str)
{
int n = str.Length;
string curr_num = "" ;
string res= "" ;
for ( int i = 0; i<n; i++)
{
while (i<n && str[i]== '0' )
i++;
while (i<n && Char.IsDigit(str[i]))
{
curr_num = curr_num + str[i];
i++;
}
if (i == n)
break ;
if (curr_num.Length > 0)
i--;
res = maximumNum(curr_num, res);
curr_num = "" ;
}
if (curr_num.Length == 0 && res.Length == 0)
res = res + '0' ;
return maximumNum(curr_num, res);
}
public static void Main()
{
string str = "100klh564abc365bg" ;
Console.WriteLine(extractMaximum(str));
}
}
|
PHP
<?php
function maximumNum( $curr_num , $res )
{
$len1 = strlen ( $curr_num );
$len2 = strlen ( $res );
if ( $len1 == $len2 )
{
$i = 0;
while ( $curr_num [ $i ]== $res [ $i ])
$i ++;
if ( $curr_num [ $i ] < $res [ $i ])
return $res ;
else
return $curr_num ;
}
return $len1 < $len2 ? $res : $curr_num ;
}
function extractMaximum( $str )
{
$n = strlen ( $str );
$curr_num = "" ;
$res = "" ;
for ( $i = 0; $i < $n ; $i ++)
{
while ( $i < $n && $str [ $i ]== '0' )
$i ++;
while ( $i < $n && $str [ $i ]>= '0' && $str [ $i ]<= '9' )
{
$curr_num .= $str [ $i ];
$i ++;
}
if ( $i == $n )
break ;
if ( strlen ( $curr_num ) > 0)
$i --;
$res = maximumNum( $curr_num , $res );
$curr_num = "" ;
}
if ( strlen ( $curr_num )== 0 && strlen ( $res )== 0)
$res .= '0' ;
return maximumNum( $curr_num , $res );
}
$str = "100klh564abc365bg" ;
echo extractMaximum( $str );
?>
|
Javascript
<script>
function maximumNum(curr_num,res)
{
let len1 = curr_num.length;
let len2 = res.length;
if (len1 == len2)
{
let i = 0;
while (curr_num[i] == res[i])
i++;
if (curr_num[i] < res[i])
return res;
else
return curr_num;
}
return len1 < len2 ? res: curr_num;
}
function extractMaximum(str)
{
let n = str.length;
let curr_num = "" ;
let res= "" ;
for (let i = 0; i<n; i++)
{
while (i<n && str[i]== '0' )
i++;
while (i<n && !isNaN(String(str[i]) * 1))
{
curr_num = curr_num + str[i];
i++;
}
if (i == n)
break ;
if (curr_num.length > 0)
i--;
res = maximumNum(curr_num, res);
curr_num = "" ;
}
if (curr_num.length == 0 && res.length == 0)
res = res + '0' ;
return maximumNum(curr_num, res);
}
let str = "100klh564abc365bg" ;
document.write(extractMaximum(str));
</script>
|
Time complexity : O(n) where n is the length of the input string. This is because each character of the string is being processed only once.
Space complexity : O(k) where k is the length of the largest numeric value present in the input string. This is because the largest numeric value is stored in a string, and the space required is proportional to its length.
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.