Javatpoint Logo
Javatpoint Logo

Check if the given string contains all the digits in Java

Introduction:

Programmers frequently get into scenarios where they must determine whether a given string contains all digits from 0 to 9. It will be helpful in various situations, including input validation, data validation, and password validation.

Problem Statement:

Write a Java program that checks if a given string consists of all the digits from 0 to 9. The program should take a string as input and output a message indicating whether the string contains all the digits. If the string carries all the digits, the message must imply that the string is valid. If the string does now not include all of the digits, the message needs to indicate that the string is invalid.

Example 1:

Input: "abc123def456ghi789jkl0".

Output: Valid.

Explanation: The fact this string consists of all of the digits from zero to nine.

Example 2:

Input: "The brief brown fox jumps over the lazy dog".

Output: Invalid

Explanation: The fact this string does not contain digits.

Example 3:

Input: "1 2 3 4 5 6 7 8 9 0".

Output: Valid

Explanation: The string consists of all the digits from zero to nine.

Approach 1: Using Iteration

ALGORITHM:

Step 1: Create a boolean array to store the presence of digits from 0 to 9.

Step 2: Initialize a flag to true to indicate that all digits are present in the string.

Step 3: Iterate through each character in the string.

Step 4: Check if the character is a digit using the Character.isDigit() method.

Step 5: If the character is a digit, set the corresponding element in the boolean array to true.

Step 6: After iterating through all the characters, iterate through the boolean array.

Step 7: If any element in the boolean array is false, set the flag to false.

Step 8: Output "Valid" if the flag is true, indicating that all digits are present in the string. Otherwise, output "InValid".

Implementation:

FileName: StringCheck.java

Output:

Valid
InValid
Valid

Complexity Analysis:

Time Complexity: O(n), where n is the length of the input string. We need to traverse through the string once to check the presence of each digit.

Space Complexity: O(1) since the presence of digits is stored in a fixed-size boolean array of size 10.

Approach 2: Using String class methods

ALGORITHM:

Step 1: Define a method called "containsAllDigits" that takes a string parameter "str" and returns a boolean.

Step 2: Iterate through the numbers 0 to 9 for each number:

Step 2.1: Check if the string contains the current number using the "contains" method and convert the number to a string using "String.valueOf(i)".

Step 2.2: If the string does not contain the current number, return false.

Step 3: If the string contains all digits from 0 to 9, return true.

Step 4: In the main method:

Step 4.1: Define three example strings: str1, str2, and str3.

Step 4.2: Check if each example string contains all digits from 0 to 9 using the "containsAllDigits" method.

Step 4.3: If the string contains all digits, output "Valid". Otherwise, output "Invalid".

FileName: StringCheck.java

Output:

Valid
Invalid
Valid

Complexity Analysis:

The time complexity of the above program is O(n * k), where n is the length of the input string and k is the number of digits (which is constant and equal to 10 in this case).

The space complexity of the program is O(1), because the program only uses a constant amount of additional space (for the boolean flag and the temporary string used to convert digits to strings).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA