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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int MAX = 10; // Function that returns true // if ch is a digit 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 9 bool 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 code int main() { string str = "Geeks12345for69708"; int len = str.length(); if (allDigits(str, len)) cout << "Yes"; else cout << "No"; return 0; } |
Java
// Java implementation of the approach class GFG { static int MAX = 10; // Function that returns true // if ch is a digit static 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 9 static 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 code public 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
# Python3 implementation of the approach MAX = 10 # Function that returns true # if ch is a digit def 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 9 def allDigits(st, le): # To mark the present digits present = [False for i in range(MAX)] # For every character of the sting 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 code st = "Geeks12345for69708"le = len(st) if (allDigits(st, le)): print("Yes") else: print("No") # This code is contributed by Mohit Kumar |
C#
// C# implementation of the approach using System; class GFG { static int MAX = 10; // Function that returns true // if ch is a digit static 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 9 static 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 code public 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 |
Yes
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:
- Minimum digits to be removed to make either all digits or alternating digits same
- How to check if string contains only digits in Java
- Python program to check if a string contains all unique characters
- Check if a binary string contains all permutations of length k
- Check if a string contains a palindromic sub-string of even length
- Lexicographically smallest permutation of a string that contains all substrings of another string
- Check if an array contains all elements of a given range
- Check if the given array contains all the divisors of some integer
- Sub-string that contains all lowercase alphabets after performing the given operation
- Length of smallest substring of a given string which contains another string as subsequence
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Print number in ascending order which contains 1, 2 and 3 in their digits.
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Check if all subarrays contains at least one unique element
- Check if the string contains consecutive letters and each letter occurs exactly once
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a string contains only alphabets in Java using ASCII values
- Check if a binary string contains consecutive same or not
- Check if a string contains two non overlapping sub-strings "geek" and "keeg"
- Program to check if a String in Java contains only whitespaces
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.

