Given a string str in which an integer is appended in the end (with or without leading zeroes). The task is to find a single digit from the range [0, 9] that must be appended in the end of the integer so that the number becomes equal to the length of remaining string. Print -1 if its not possible.
Input: str = “geeksforgeeks1”
Length of “geeksforgeeks” is 13
So, 3 must be appended at the end of 1.
Input: str = “abcd0”
Approach: Find the number appended in the end of the string say num and append a 0 in the end which is the least digit possible i.e. num = num * 10. Now find the length of the remaining string ignoring the numeric from the end say len. Now the digit which must be appended will be digit = len – num. If digit is in the range [0, 9] then print it else print -1.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Append digits to the end of dupicate strings to make all strings in an array unique
- Area of plot remaining at the end
- Count of N-digit numbers having digit XOR as single digit
- Count of Numbers in Range where first digit is equal to last digit of the number
- Length of longest substring to be deleted to make a string equal to another string
- Minimum move to end operations to make all strings equal
- Minimum characters to be deleted from the end to make given two strings equal
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Append two elements to make the array satisfy the given condition
- std::string::append vs std::string::push_back() vs Operator += in C++
- Length of remaining two sides of a Triangle from a given side and its adjacent angles
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Check if the array has an element which is equal to sum of all the remaining elements
- Check if the array has an element which is equal to product of remaining elements
- Element equal to the sum of all the remaining elements
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Perform n steps to convert every digit of a number in the format [count][digit]
- Largest number less than N with digit sum greater than the digit sum of N
- Find the remainder when First digit of a number is divided by its Last digit
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.