VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-length-string-equal-number-appended-last/

⇱ Check length of a string is equal to the number appended at its last - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check length of a string is equal to the number appended at its last

Last Updated : 23 Jul, 2025

Given a string that (may) be appended with a number at last. You need to find whether the length of string excluding that number is equal to that number. For example for "helloworld10", answer is True as helloworld consist of 10 letters. Length of String is less than 10, 000.

Examples :

Input:  str = "geeks5"
Output: Yes
Explanation : As geeks is of 5 length and at
last number is also 5.
Input: str = "geeksforgeeks15"
Output: No
Explanation: As geeksforgeeks is of 13 length and
at last number is 15 i.e. not equal

Asked in: Codenation Interview

A Naive approach is to traverse from starting and retrieve the number from string and check if length of string - digits in the number = number or Not 

An efficient method is to do following steps  

  1. Traverse string from end and keep storing the number till it is smaller than the length of the overall string.
  2. If the number is equal to length of string except that number's digits then return true. 
  3. Else return false.

Implementation:


Output
Yes

Time complexity: O(n) where n is length of the string
Auxiliary space:O(1)

Approach#2: Using is.digit()

First, the length of the input string is calculated, and 1 is subtracted from it to exclude the last character, which is expected to be a number. Then, the isdigit() method is used to check if the last character of the string is a digit. If the last character is a digit, the code converts it into an integer and compares it with the length of the string. If they are equal, it means the length of the string is equal to the number appended at the end of the string.

Algorithm

1. Initialize a variable str with the input string.
2. Calculate the length of the string and store it in the variable length.
3. Subtract 1 from length to exclude the last character from the length calculation.
4. Check if the last character of the string is a digit using the isdigit() method.
5. If the last character is a digit, convert it into an integer using the int() function.
6. Compare the integer with length.
7. If they are equal, print "Yes"; otherwise, print "No".


Output
No

Time Complexity: O(1), The algorithm has constant time complexity because it performs a fixed number of operations, regardless of the length of the input string.
Space Complexity: O(1), The algorithm uses a fixed amount of memory to store the input string, length, and a few variables, so the space complexity is constant.

Regrex Approach :

Follow the given steps to solve the problem :

  • Use regular expressions to separate the string from the number at the end
  • Extract the string part and the number part
  • Convert the number part to an integer
  • Compare the length of the string part with the number
  • Return Yes if they are equal, No otherwise.

Below is the implementation of the above approach:


Output
Yes

Time Complexity: O(n), where n is the length of the input string.

Auxiliary Space: O(1)


Comment
Article Tags: