Given a string str consisting of alphanumeric characters, the task is to check whether the string contains all the digits from 1 to 9. The string can contain other characters, but it should contain all the digits from 1 to 9.
Examples:
Input: str = “Geeks12345for69708”
Output: Yes
Explanation: All the digits from 0 to 9 are present in the given string.
Input: str = “Amazing1234”
Output: No
Explanation: All the digits are not present in the string.
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++
#include <bits/stdc++.h>
using namespace std;
const int MAX = 10;
bool isDigit( char ch)
{
if (ch >= '0' && ch <= '9' )
return true ;
return false ;
}
bool allDigits(string str, int len)
{
bool present[MAX] = { false };
for ( int i = 0; i < len; i++) {
if (isDigit(str[i])) {
int digit = str[i] - '0' ;
present[digit] = true ;
}
}
for ( int i = 0; i < MAX; i++) {
if (!present[i])
return false ;
}
return true ;
}
int main()
{
string str = "Geeks12345for69708" ;
int len = str.length();
if (allDigits(str, len))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static int MAX = 10 ;
static boolean isDigit( char ch)
{
if (ch >= '0' && ch <= '9' )
return true ;
return false ;
}
static boolean allDigits(String str, int len)
{
boolean []present = new boolean [MAX];
for ( int i = 0 ; i < len; i++)
{
if (isDigit(str.charAt(i)))
{
int digit = str.charAt(i) - '0' ;
present[digit] = true ;
}
}
for ( int i = 0 ; i < MAX; i++)
{
if (!present[i])
return false ;
}
return true ;
}
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" );
}
}
|
Python3
MAX = 10
def isDigit(ch):
ch = ord (ch)
if (ch > = ord ( '0' ) and ch < = ord ( '9' )):
return True
return False
def allDigits(st, le):
present = [ False for i in range ( MAX )]
for i in range (le):
if (isDigit(st[i])):
digit = ord (st[i]) - ord ( '0' )
present[digit] = True
for i in range ( MAX ):
if (present[i] = = False ):
return False
return True
st = "Geeks12345for69708"
le = len (st)
if (allDigits(st, le)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static int MAX = 10;
static bool isDigit( char ch)
{
if (ch >= '0' && ch <= '9' )
return true ;
return false ;
}
static bool allDigits(String str, int len)
{
bool []present = new bool [MAX];
for ( int i = 0; i < len; i++)
{
if (isDigit(str[i]))
{
int digit = str[i] - '0' ;
present[digit] = true ;
}
}
for ( int i = 0; i < MAX; i++)
{
if (!present[i])
return false ;
}
return true ;
}
public static void Main(String[] args)
{
String str = "Geeks12345for69708" ;
int len = str.Length;
if (allDigits(str, len))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
let MAX = 10;
function isDigit(ch)
{
if (ch >= '0' && ch <= '9' )
return true ;
return false ;
}
function allDigits(str, len)
{
let present = Array.from({length: MAX}, (_, i) => 0);
for (let i = 0; i < len; i++)
{
if (isDigit(str[i]))
{
let digit = str[i] - '0' ;
present[digit] = true ;
}
}
for (let i = 0; i < MAX; i++)
{
if (!present[i])
return false ;
}
return true ;
}
let str = "Geeks12345for69708" ;
let len = str.length;
if (allDigits(str, len))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
29 Dec, 2022
Like Article
Save Article