Robert is given a string that ends with a character or a number. He has to check the length of the given string and append it to the end in such a way that if the original string ends with a number then the new string with the appended value must have the length of the string as the last two characters. if the string ends with characters then append the total length as the last character. also, the number to be appended must be a single positive digit.
The output displays the single-digit value to be appended if the value is more than one digit or no digit is to be appended then print -1
Input:
abcdefghijklmnop1
Output:
6
Solution
step-by-step approach to solving this problem:
- Read the input string from the user.
- Iterate over the string from the end and extract the numeric value at the end. Keep multiplying this numeric value by 10 and adding the next digit until we encounter a non-digit character.
- If the extracted numeric value is greater than 0, it means the input string ends with a numeric value. Multiply this numeric value by 10 to get the expected length of the input string, ignoring the numeric value at the end.
- Calculate the length of the remaining string (excluding the numeric value at the end) by subtracting the numeric value from the total length of the input string.
- Subtract the numeric value from the length of the remaining string to get the single-digit value that needs to be appended.
- If the calculated value is a single-digit number between 0 and 9, append it to the input string along with a 0 at the end to create the final output string.
- If the calculated value is not a single-digit number between 0 and 9, output -1 indicates that we couldn’t find a valid digit to append.
- Output the final output string or -1 as the result of the program.
//java
import java.util.Scanner;
public class Rt {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
int num = 0;
int len = 0;
int i = input.length() - 1;
while (i >= 0 && Character.isDigit(input.charAt(i))) {
num = num * 10 + input.charAt(i) - '0';
i--;
}
if (num > 0) {
num *= 10;
len = input.length() - num / 10;
int digit = len - num;
if (digit >= 0 && digit <= 9) {
// sauravhathi
String output = input.substring(0, input.length() - 1) + digit + "0";
System.out.println(digit);
return;
}
}
System.out.println(-1);
}
}
// c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cin >> input;
int num = 0;
int len = 0;
int i = input.length() - 1;
while (i >= 0 && isdigit(input[i])) {
num = num * 10 + input[i] - '0';
i--;
}
if (num > 0) {
num *= 10;
len = input.length() - num / 10;
int digit = len - num;
if (digit >= 0 && digit <= 9) {
string output = input.substr(0, input.length() - 1) + to_string(digit) + "0";
cout << digit << endl;
// sauravhathi
return 0;
}
}
cout << -1 << endl;
return 0;
}
// python
input = input()
num = 0
size = len(input)
i = size - 1
while i >= 0 and input[i].isdigit():
num = num * 10 + int(input[i])
i -= 1
if num > 0:
num *= 10
len = size - num // 10
digit = len - num
if digit >= 0 and digit <= 9:
output = input[:i + 1] + str(digit) + "0"
print(digit)
# sauravhathi
exit(0)
print(-1)
Happy Learning – If you require any further information, feel free to contact me.