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

06 Jul, 2022
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  
Traverse string from end and keep storing the number till it is smaller than the length of the overall string.If the number is equal to length of string except that number’s digits then return true. Else return false.
Traverse string from end and keep storing the number till it is smaller than the length of the overall string.
If the number is equal to length of string except that number’s digits then return true. 
Else return false.
Implementation:
C++
Java
Python3
C#
PHP
Javascript
// C++ program to check if size of string is appended// at the end or not.#include <bits/stdc++.h>using namespace std; // Function to find if given number is equal to// length or notbool isequal(string str){    int n = str.length();     // Traverse string from end and find the number    // stored at the end.    // x is used to store power of 10.    int num = 0, x = 1, i = n - 1;    for (i = n - 1; i >= 0; i--) {        if ('0' <= str[i] && str[i] <= '9') {            num = (str[i] - '0') * x + num;            x = x * 10;            if(num>=n)                return false;        }        else            break;    }     // Check if number is equal to string length except    // that number's digits    return num == i + 1;} // Drivers codeint main(){    string str = "geeksforgeeks13";    isequal(str) ? cout << "Yes" : cout << "No";    return 0;}
// Java program to check if size of// string is appended at the end or not.import java.io.*; class GFG {     // Function to find if given number is    // equal to length or not    static boolean isequal(String str)    {        int n = str.length();         // Traverse string from end and find the number        // stored at the end.        // x is used to store power of 10.        int num = 0, x = 1, i = n - 1;        for (i = n - 1; i >= 0; i--)        {            if ('0' <= str.charAt(i) &&                str.charAt(i) <= '9')            {                num = (str.charAt(i) - '0') * x + num;                x = x * 10;                if(num>=n)                    return false;            }            else                break;        }         // Check if number is equal to string        // length except that number's digits        return num == i + 1;    }     // Drivers code    static public void main(String[] args)    {        String str = "geeksforgeeks13";        if (isequal(str))            System.out.println("Yes");        else            System.out.println("No");    }} // This Code is contributed by vt_m.
# Python 3 program to check if size of# string is appended at the end or not. # Function to find if given number# is equal to length or notdef isequal(str):     n = len(str)     # Traverse string from end and    # find the number stored at the end.    # x is used to store power of 10.    num = 0    x = 1    i = n - 1    for i in range(n - 1, -1,-1) :        if ('0' <= str[i] and str[i] <= '9') :            num = (ord(str[i]) - ord('0')) * x + num            x = x * 10            if (num>=n):                return false             else:            break     # Check if number is equal to string    # length except that number's digits    return num == i + 1 # Driver Codeif __name__ == "__main__":         str = "geeksforgeeks13"    print("Yes") if isequal(str) else print("No") # This code is contributed by ChitraNayal
// C# program to check if size of// string is appended at the end or not.using System; class GFG {     // Function to find if given number    // is equal to length or not    static bool isequal(string str)    {        int n = str.Length;         // Traverse string from end and find the number        // stored at the end.        // x is used to store power of 10.        int num = 0, x = 1, i = n - 1;        for (i = n - 1; i >= 0; i--)        {            if ('0' <= str[i] && str[i] <= '9') {                num = (str[i] - '0') * x + num;                x = x * 10;                if(num>=n)                    return false;            }            else                break;        }         // Check if number is equal to string        // length except that number's digits        return num == i + 1;    }     // Drivers code    static public void Main()    {        string str = "geeksforgeeks13";        if (isequal(str))            Console.WriteLine("Yes");        else            Console.WriteLine("No");    }} // This Code is contributed by vt_m.
<?php// PHP program to check if size// of string is appended at// the end or not. // Function to find if given// number is equal to length or notfunction isequal($str){    $n = strlen($str);     // Traverse string from end    // and find the number stored    // at the end. x is used to    // store power of 10.    $num = 0; $x = 1; $i = $n - 1;    for ($i = $n - 1; $i >= 0; $i--)    {        if ('0' <= $str[$i] &&                   $str[$i] <= '9')        {            $num = ($str[$i] - '0') *                           $x + $num;            $x = $x * 10;            if($num>=$n)                return false;        }        else            break;    }     // Check if number is equal    // to string length except    // that number's digits    return $num == $i + 1;} // Driver code$str = "geeksforgeeks13";if(isequal($str))echo "Yes" ;elseecho "No";return 0; // This code is contributed by nitin mittal.?>
<script> // Javascript program to check if size of// string is appended at the end or not. // Function to find if given number is// equal to length or notfunction isequal(str){    let n = str.length;     // Traverse string from end and find    // the number stored at the end.    // x is used to store power of 10.    let num = 0, x = 1, i = n - 1;    for(i = n - 1; i >= 0; i--)    {        if ('0' <= str[i] &&            str[i] <= '9')        {            num = (str[i] - '0') * x + num;            x = x * 10;                         if (num >= n)                return false;        }        else            break;    }     // Check if number is equal to string    // length except that number's digits    return num == i + 1;} // Driver codelet str = "geeksforgeeks13"; if (isequal(str))    document.write("Yes");else    document.write("No"); // This code is contributed by rag2127 </script>
Output : 
Yes
Time complexity: O(n) where n is length of the stringAuxiliary space: O(1)
This article is contributed by Sahil Chhabra (akku). 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. 
vt_m
nitin mittal
ukasp
AshutoshPrasad2
rag2127
shaheeneallamaiqbal
hardikkoriintern
Codenation
Strings
Codenation
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.