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

给定一个字符串,最后可以加上一个数字。您需要找出不包括该数字的字符串的长度是否等于该数字。例如,对于“helloworld10”,答案是正确的,因为helloworld由10个字母组成。绳子的长度小于10000。

null

例如:

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

被问到: 编码面试

A. 天真的方法 是从开始遍历并从字符串中检索数字,并检查字符串的长度–数字中的数字是否=数字

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

  1. 从字符串的末尾开始遍历,并一直存储数字,直到它小于整个字符串的长度。
  2. 如果数字等于字符串的长度(数字除外),则返回true。
  3. 否则返回false。

C++

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

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


Javascript

<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 not
function 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 code
let str = "geeksforgeeks13" ;
if (isequal(str))
document.write( "Yes" );
else
document.write( "No" );
// This code is contributed by rag2127
</script>


输出:

Yes

本文由 萨希尔·查布拉(阿克库) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享