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.
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.
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Append a digit in the end to make the number equal to the length of the remaining string
- Check if a string contains a palindromic sub-string of even length
- Check if a binary string contains all permutations of length k
- Check if string can be rearranged so that every Odd length Substring is Palindrome
- Check if the frequency of any character is more than half the length of the string
- Program to check if first and the last characters of string are equal
- String Range Queries to find the number of subsets equal to a given String
- Number of strings of length N with no palindromic sub string
- Check if a large number can be divided into two or more segments of equal sum
- Count number of equal pairs in a string
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Length of the longest substring with equal 1s and 0s
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Find length of longest subsequence of one string which is substring of another string