40. longestDigitsPrefix


Task

Given a string, output its longest prefix which contains only digits.

A substring of a string is called a prefix if it starts at the string’s first character.

Example

For inputString = "123aa1", the output should be
longestDigitsPrefix(inputString) = "123".

Input/Output

  • [execution time limit]
    4 seconds (py3)

  • [input] string inputString
    Guaranteed constraints: 3 ≤ inputString.length ≤ 100.

  • [output] string

My Solution

def longestDigitsPrefix(inputString):
    if not inputString[0].isdigit():
        return ""
    for i in range(len(inputString)):
        if not inputString[i].isdigit():
            return inputString[:i]
    return inputString