Form Integers – Divisible by 25

The program must accept a string S representing a number. The string S contains digits, underscores and X‘s. The program must print the number of possible integers that can be formed from the string S based on the following conditions.
– All the underscores must be replaced with the digits.
– All X’s must be replaced with a same digit.
– The integer must be divisible by 25 and does not contain any leading zeroes.

Boundary Condition(s):
1 <= Length of S <= 7

Input Format:
The first line contains S.

Output Format:
The first line contains an integer value representing the number of possible integers that can be formed from the string S.

Example Input/Output 1:
Input:
8_XX

Output:
10

Explanation:
Here the given string is 8_XX.
The 10 possible integers are given below.
8000
8100
8200
8300
8400
8500
8600
8700
8800
8900

Example Input/Output 2:
Input:
_25

Output:
9

Example Input/Output 3:
Input:
0_75

Output:
0

Explanation:
Here the given string is 0_75.
There is no way to form such integers as the given string starts with 0.

Example Input/Output 4:
Input:
0

Output:
1

Explanation:
Here the given string is 0.
The only integer that can be formed is 0 which is divisible by 25.

n=input().strip()
if n.find('X')==-1 and n.find('_')==-1:
    print("1") if int(n)%25==0 else print("0")
    exit()
k=10**(len(n)-1)
l=[str(i) for i in range(k if len(n)!=2 else 25,k*10,25)]
count=0
for i in range(len(l)):
    k2=n.find('X')
    k3=l[i][k2]
    k1=n
    if k2>=0:
        k1=k1.replace('X',k3)
    for j in range(len(k1)):
        if k1[j]=='_':
            continue
        elif k1[j]!=l[i][j]:
            break
    else:
        count+=1
print(count)
#include<stdio.h>
#include<stdlib.h>

int main()
{
    char a[8],num[8];int arr[8],arr1[8],y=0,x=0,s=1,d=0
    ,ct=0,h=0;
    scanf("%s",&a);
    if(strlen(a)==1){
        printf("1");return;
    }
    for(int i=0;i<strlen(a);i++){
        if(a[i]=='X'){
            arr[y++]=i;
        }else if(isdigit(a[i])){
           arr1[x++]=i;
        }
    }int l=strlen(a);
    for(int i=1;i<=l;i++){
        s*=10;
    }
    for(int i=s/10;i<s;i++){
        d=0,h=0;
        if(i%25==0){
            sprintf(num,"%d",i);
            for(int j=0;j<y-1;j++){
                if(num[arr[j]]!=num[arr[j+1]]){
                    d=1;break;
                }
            }
            for(int j=0;j<x;j++){
                if(a[arr1[j]]!=num[arr1[j]]){
                    h=1;break;
                }
            }
            if(d==0 && h==0){
                if(!isdigit(a[0]) || a[0]=='0'){
                    ct++;
                }else if(isdigit(a[0])){
                    int t=a[0]-'0';
                    int t1=num[0]-'0';
                    if(t==t1){
                        ct++;
                    }
                }
            }
        }
    }
    printf("%d",ct);
}
#include <bits/stdc++.h>

using namespace std;

int ans=0;
void solve(string s, int pos){
    if(s[0]=='0' && s.size()>1) return;
    if(pos==s.size()){
        int f=1;
        for(char i='0'; i<='9' && f; i++){
            f=0;
            string t=s;
            for(int j=0; j<pos; j++){
                if(s[j]=='X'){
                    f=1;
                    t[j]=i;
                }
            }
            if(t[0]=='0' && t.size()>1) continue;
            int val = stoi(t);
            if(val%25==0) ans++;
        }
        return;
    }
    if(s[pos]=='_'){
        for(char c='0'; c<='9'; c++){
            s[pos]=c;
            solve(s, pos+1);
        }
    }else{
        solve(s, pos+1);
    }
}
int main(int argc, char** argv)
{
    string s; cin >> s;
    solve(s, 0);
    cout << ans ;
}
import java.util.*;
public class Hello{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        Queue<String> q = new LinkedList<>();
        if(s.contains("X")){
            for(int i=0;i<10;i++) q.add(s.replace("X",i+""));
        }else q.add(s);
        int count=0;
        while(!q.isEmpty()){
            String curr = q.poll();
            if(curr.contains("_")){
                int ind = curr.indexOf("_");
                char[] arr = curr.toCharArray();
                for(int i=0;i<10;i++){
                    arr[ind] = (char)('0'+i);
                    String sub = String.valueOf(arr);
                    q.add(sub);
                }
            }else{
                if(isValid(curr)) count++;
            }
        }
        System.out.print(count);
    }
    static boolean isValid(String s){
        return (s.length()==1||s.charAt(0)!='0')&&Integer.parseInt(s)%25==0;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.