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.
Examples:
Input: str = “geeksforgeeks1”
Output: 3
Length of “geeksforgeeks” is 13
So, 3 must be appended at the end of 1.Input: str = “abcd0”
Output: 4
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:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the requried digit int find_digit(string s, int n) { // To store the position of the first // numeric digit in the string int first_digit = -1; for (int i = n - 1; i >= 0; i--) { if (s[i] < '0' || s[i] > '9') { first_digit = i; break; } } first_digit++; // To store the length of the // string without the numeric // digits in the end int s_len = first_digit; // pw stores the current power of 10 // and num is to store the number // which is appended in the end int num = 0, pw = 1; int i = n - 1; while (i >= 0) { // If current character is // a numeric digit if (s[i] >= '0' && s[i] <= '9') { // Get the current digit int digit = s[i] - '0'; // Build the number num = num + (pw * digit); // If number exceeds the length if (num >= s_len) return -1; // Next power of 10 pw = pw * 10; } i--; } // Append 0 in the end num = num * 10; // Required number that must be added int req = s_len - num; // If number is not a single digit if (req > 9 || req < 0) return -1; return req; } // Driver code int main() { string s = "abcd0"; int n = s.length(); cout << find_digit(s, n); return 0; } |
Java
// Java implementation of the approach import java.io.*; class GFG { // Function to return the requried digit static int find_digit(String s, int n) { // To store the position of the first // numeric digit in the string int first_digit = -1; for (int i = n - 1; i >= 0; i--) { if (s.charAt(i) < '0' || s.charAt(i) > '9') { first_digit = i; break; } } first_digit++; // To store the length of the // string without the numeric // digits in the end int s_len = first_digit; // pw stores the current power of 10 // and num is to store the number // which is appended in the end int num = 0, pw = 1; int i = n - 1; while (i >= 0) { // If current character is // a numeric digit if (s.charAt(i) >= '0' && s.charAt(i) <= '9') { // Get the current digit int digit = s.charAt(i) - '0'; // Build the number num = num + (pw * digit); // If number exceeds the length if (num >= s_len) return -1; // Next power of 10 pw = pw * 10; } i--; } // Append 0 in the end num = num * 10; // Required number that must be added int req = s_len - num; // If number is not a single digit if (req > 9 || req < 0) return -1; return req; } // Driver code public static void main (String[] args) { String s = "abcd0"; int n = s.length(); System.out.print(find_digit(s, n)); } } // This code is contributed by vt_m |
Python3
# Python3 implementation of the approach # Function to return the requried digit def find_digit(s, n): # To store the position of the first # numeric digit in the string first_digit = -1 for i in range(n - 1, -1, -1): if s[i] < '0' or s[i] > '9': first_digit = i break first_digit += 1 # To store the length of the # string without the numeric # digits in the end s_len = first_digit num = 0 pw = 1 i = n - 1 while i >= 0: # If current character is # a numeric digit if s[i] >= '0' and s[i] <= '9': # Get the current digit digit = ord(s[i]) - ord('0') # Build the number num = num + (pw * digit) # If number exceeds the length if num >= s_len: return -1 # Next power of 10 pw = pw * 10 i -= 1 # Append 0 in the end num = num * 10 # Required number that must be added req = s_len - num # If number is not a single digit if req > 9 or req < 0: return -1 return req # Driver code if __name__ == "__main__": s = "abcd0" n = len(s) print(find_digit(s, n)) # This code is contributed by # sanjeev2552 |
C#
// C# implementation of the approach using System; class GFG { // Function to return the requried digit static int find_digit(String s, int n) { // To store the position of the first // numeric digit in the string int first_digit = -1, i; for (i = n - 1; i >= 0; i--) { if (s[i] < '0' || s[i] > '9') { first_digit = i; break; } } first_digit++; // To store the length of the // string without the numeric // digits in the end int s_len = first_digit; // pw stores the current power of 10 // and num is to store the number // which is appended in the end int num = 0, pw = 1; i = n - 1; while (i >= 0) { // If current character is // a numeric digit if (s[i] >= '0' && s[i] <= '9') { // Get the current digit int digit = s[i] - '0'; // Build the number num = num + (pw * digit); // If number exceeds the length if (num >= s_len) return -1; // Next power of 10 pw = pw * 10; } i--; } // Append 0 in the end num = num * 10; // Required number that must be added int req = s_len - num; // If number is not a single digit if (req > 9 || req < 0) return -1; return req; } // Driver code public static void Main (String[] args) { String s = "abcd0"; int n = s.Length; Console.Write(find_digit(s, n)); } } // This code is contributed by PrinciRaj1992 |
4
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.
Recommended Posts:
- Area of plot remaining at the end
- Count of N-digit numbers having digit XOR as single digit
- Length of longest substring to be deleted to make a string equal to another string
- Count of Numbers in Range where first digit is equal to last digit of the number
- Minimum move to end operations to make all strings equal
- Minimum characters to be deleted from the end to make given two strings equal
- Append two elements to make the array satisfy the given condition
- Length of remaining two sides of a Triangle from a given side and its adjacent angles
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- 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
- std::string::append vs std::string::push_back() vs Operator += in C++
- Number of ways to remove a sub-string from S such that all remaining characters are same
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Find the last remaining Character in the Binary String according to the given conditions
- Converting one string to other using append and delete last operations
- Form N-copy string with add, remove and append operations
- How to Append a Character to a String in C
- Perform n steps to convert every digit of a number in the format [count][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 contribute@geeksforgeeks.org. 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.

