📌  相关文章
📜  检查字符串的长度是否等于最后附加的数字

📅  最后修改于: 2022-05-13 01:57:07.954000             🧑  作者: Mango

检查字符串的长度是否等于最后附加的数字

给定一个最后(可能)附加一个数字的字符串。您需要查找不包括该数字的字符串长度是否等于该数字。例如对于“helloworld10”,答案是 True,因为 helloworld 由 10 个字母组成。 String 的长度小于 10, 000。

例子 :

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

被问到:Codenation 面试

一种天真的方法是从开始遍历并从字符串中检索数字并检查字符串的长度 - 数字中的数字 = 数字还是不

一种有效的方法是执行以下步骤

  1. 从末尾遍历字符串并继续存储数字,直到它小于整个字符串的长度。
  2. 如果该数字等于除该数字的数字之外的字符串长度,则返回 true。
  3. 否则返回假。
C++
// C++ program to check if size of string is appended
// at the end or not.
#include 
using namespace std;
 
// Function to find if given number is equal to
// length or not
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
int main()
{
    string str = "geeksforgeeks13";
    isequal(str) ? cout << "Yes" : cout << "No";
    return 0;
}


Java
// 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.


Python3
# 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 not
def 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 Code
if __name__ == "__main__":
     
    str = "geeksforgeeks13"
    print("Yes") if isequal(str) else print("No")
 
# This code is contributed by ChitraNayal


C#
// 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
= 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" ;
else
echo "No";
return 0;
 
// This code is contributed by nitin mittal.
?>


Javascript


输出 :

Yes