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 longest length and not largest value.
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 digit is encountered, store its length and match it with the length previously find series of digit to find out the maximum of all the continuous series of digits.
Below is the implementation of above approach.
Time Complexity: O(n)
Auxiliary Space Complexity: O(1)
- Find number of magical pairs of string of length L
- C program to find the length of a string
- Program to find Length of Bridge using Speed and Length of Train
- The length of a string using pointers
- Count of sub-strings of length n possible from the given string
- Python program to print even length words in a string
- Longest string in non-decreasing order of ASCII code and in arithmetic progression
- Reduce string to shortest length by deleting a pair of same adjacent characters
- Find the Diameter or Longest chord of a Circle
- Find length of Diagonal of Hexagon
- Find if a string starts and ends with another given string
- Given number of matches played, find number of teams in tournament
- Find the number of ways to divide number into four parts such that a = c and b = d
- Find count of digits in a number that divide the number
- Given equation of a circle as string, find area
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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