Check if given string contains all the digits - GeeksforGeeks

04 Aug, 2021
Given a string str consisting of alphanumeric characters, the task is to check whether the string contains all the digits from 1 to 9.
Examples: 
Input: str = “Geeks12345for69708” Output: Yes All the digits from 0 to 9 are present in the given string.
Input: str = “Amazing1234” Output: No  
Approach: Create a frequency array to mark the frequency of each of the digits from 0 to 9. Finally, traverse the frequency array and if there is any digit that is not present in the given string then the answer will be “No” else the answer will be “Yes”.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; const int MAX = 10; // Function that returns true// if ch is a digitbool isDigit(char ch){    if (ch >= '0' && ch <= '9')        return true;    return false;} // Function that returns true// if str contains all the// digits from 0 to 9bool allDigits(string str, int len){     // To mark the present digits    bool present[MAX] = { false };     // For every character of the string    for (int i = 0; i < len; i++) {         // If the current character is a digit        if (isDigit(str[i])) {             // Mark the current digit as present            int digit = str[i] - '0';            present[digit] = true;        }    }     // For every digit from 0 to 9    for (int i = 0; i < MAX; i++) {         // If the current digit is        // not present in str        if (!present[i])            return false;    }     return true;} // Driver codeint main(){    string str = "Geeks12345for69708";    int len = str.length();     if (allDigits(str, len))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of the approachclass GFG{ static int MAX = 10; // Function that returns true// if ch is a digitstatic boolean isDigit(char ch){    if (ch >= '0' && ch <= '9')        return true;    return false;} // Function that returns true// if str contains all the// digits from 0 to 9static boolean allDigits(String str, int len){     // To mark the present digits    boolean []present = new boolean[MAX];     // For every character of the String    for (int i = 0; i < len; i++)    {         // If the current character is a digit        if (isDigit(str.charAt(i)))        {             // Mark the current digit as present            int digit = str.charAt(i) - '0';            present[digit] = true;        }    }     // For every digit from 0 to 9    for (int i = 0; i < MAX; i++)    {         // If the current digit is        // not present in str        if (!present[i])            return false;    }     return true;} // Driver codepublic static void main(String[] args){    String str = "Geeks12345for69708";    int len = str.length();     if (allDigits(str, len))        System.out.print("Yes");    else        System.out.print("No");}} // This code is contributed by 29AjayKumar
# Python3 implementation of the approachMAX = 10 # Function that returns true# if ch is a digitdef isDigit(ch):    ch = ord(ch)    if (ch >= ord('0') and ch <= ord('9')):        return True    return False # Function that returns true# if st contains all the# digits from 0 to 9def allDigits(st, le):     # To mark the present digits    present = [False for i in range(MAX)]     # For every character of the string    for i in range(le):         # If the current character is a digit        if (isDigit(st[i])):             # Mark the current digit as present            digit = ord(st[i]) - ord('0')            present[digit] = True     # For every digit from 0 to 9    for i in range(MAX):         # If the current digit is        # not present in st        if (present[i] == False):            return False     return True # Driver codest = "Geeks12345for69708"le = len(st) if (allDigits(st, le)):    print("Yes")else:    print("No") # This code is contributed by Mohit Kumar
// C# implementation of the approachusing System; class GFG{ static int MAX = 10; // Function that returns true// if ch is a digitstatic bool isDigit(char ch){    if (ch >= '0' && ch <= '9')        return true;    return false;} // Function that returns true// if str contains all the// digits from 0 to 9static bool allDigits(String str, int len){     // To mark the present digits    bool []present = new bool[MAX];     // For every character of the String    for (int i = 0; i < len; i++)    {         // If the current character is a digit        if (isDigit(str[i]))        {             // Mark the current digit as present            int digit = str[i] - '0';            present[digit] = true;        }    }     // For every digit from 0 to 9    for (int i = 0; i < MAX; i++)    {         // If the current digit is        // not present in str        if (!present[i])            return false;    }     return true;} // Driver codepublic static void Main(String[] args){    String str = "Geeks12345for69708";    int len = str.Length;     if (allDigits(str, len))        Console.Write("Yes");    else        Console.Write("No");}} // This code is contributed by 29AjayKumar
<script> // JavaScript implementation of the approach let MAX = 10;   // Function that returns true// if ch is a digitfunction isDigit(ch){    if (ch >= '0' && ch <= '9')        return true;    return false;}   // Function that returns true// if str contains all the// digits from 0 to 9function allDigits(str, len){       // To mark the present digits    let present = Array.from({length: MAX}, (_, i) => 0);       // For every character of the String    for (let i = 0; i < len; i++)    {           // If the current character is a digit        if (isDigit(str[i]))        {               // Mark the current digit as present            let digit = str[i] - '0';            present[digit] = true;        }    }       // For every digit from 0 to 9    for (let i = 0; i < MAX; i++)    {           // If the current digit is        // not present in str        if (!present[i])            return false;    }       return true;} // Driver code           let str = "Geeks12345for69708";    let len = str.length;       if (allDigits(str, len))        document.write("Yes");    else        document.write("No");                                                                                 </script>
Yes
 
mohit kumar 29
29AjayKumar
splevel62
simranarora5sos
frequency-counting
Arrays
Hash
Searching
Strings
Arrays
Searching
Hash
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Introduction to Arrays
Linked List vs Array
Given an array of size n and a number k, find all elements that appear more than n/k times
Queue | Set 1 (Introduction and Array Implementation)
Find the Missing Number
Internal Working of HashMap in Java
Hashing | Set 1 (Introduction)
Count pairs with given sum
Hashing | Set 3 (Open Addressing)
Hashing | Set 2 (Separate Chaining)