Append a digit in the end to make the number equal to the length of the remaining string
Last Updated :
04 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int find_digit(string s, int n)
{
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++;
int s_len = first_digit;
int num = 0, pw = 1;
int i = n - 1;
while (i >= 0) {
if (s[i] >= '0' && s[i] <= '9' ) {
int digit = s[i] - '0' ;
num = num + (pw * digit);
if (num >= s_len)
return -1;
pw = pw * 10;
}
i--;
}
num = num * 10;
int req = s_len - num;
if (req > 9 || req < 0)
return -1;
return req;
}
int main()
{
string s = "abcd0" ;
int n = s.length();
cout << find_digit(s, n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int find_digit(String s, int n)
{
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++;
int s_len = first_digit;
int num = 0 , pw = 1 ;
int i = n - 1 ;
while (i >= 0 )
{
if (s.charAt(i) >= '0' &&
s.charAt(i) <= '9' )
{
int digit = s.charAt(i) - '0' ;
num = num + (pw * digit);
if (num >= s_len)
return - 1 ;
pw = pw * 10 ;
}
i--;
}
num = num * 10 ;
int req = s_len - num;
if (req > 9 || req < 0 )
return - 1 ;
return req;
}
public static void main (String[] args)
{
String s = "abcd0" ;
int n = s.length();
System.out.print(find_digit(s, n));
}
}
|
Python3
def find_digit(s, n):
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
s_len = first_digit
num = 0
pw = 1
i = n - 1
while i > = 0 :
if s[i] > = '0' and s[i] < = '9' :
digit = ord (s[i]) - ord ( '0' )
num = num + (pw * digit)
if num > = s_len:
return - 1
pw = pw * 10
i - = 1
num = num * 10
req = s_len - num
if req > 9 or req < 0 :
return - 1
return req
if __name__ = = "__main__" :
s = "abcd0"
n = len (s)
print (find_digit(s, n))
|
C#
using System;
class GFG
{
static int find_digit(String s, int n)
{
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++;
int s_len = first_digit;
int num = 0, pw = 1;
i = n - 1;
while (i >= 0)
{
if (s[i] >= '0' &&
s[i] <= '9' )
{
int digit = s[i] - '0' ;
num = num + (pw * digit);
if (num >= s_len)
return -1;
pw = pw * 10;
}
i--;
}
num = num * 10;
int req = s_len - num;
if (req > 9 || req < 0)
return -1;
return req;
}
public static void Main (String[] args)
{
String s = "abcd0" ;
int n = s.Length;
Console.Write(find_digit(s, n));
}
}
|
Javascript
<script>
function find_digit(s, n)
{
var first_digit = -1;
for ( var i = n - 1; i >= 0; i--) {
if (s[i] < '0' || s[i] > '9' ) {
first_digit = i;
break ;
}
}
first_digit++;
var s_len = first_digit;
var num = 0, pw = 1;
var i = n - 1;
while (i >= 0) {
if (s[i] >= '0' && s[i] <= '9' ) {
var digit = s[i] - '0' ;
num = num + (pw * digit);
if (num >= s_len)
return -1;
pw = pw * 10;
}
i--;
}
num = num * 10;
var req = s_len - num;
if (req > 9 || req < 0)
return -1;
return req;
}
var s = "abcd0" ;
var n = s.length;
document.write( find_digit(s, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...