Check if the given string contains all the digits in Java10 Sept 2024 | 4 min read 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 IterationALGORITHM: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 methodsALGORITHM: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). Next TopicConstructor in Abstract Class in Java |
It is a fundamental necessity to send e-mails, no matter what platform you are using-Python, JavaEE, Java, etc. It can be necessary to send e-mails to confirm registration or sign up or to issue error notices. Writing Java applications allows you to send e-mails using Java. 1....
4 min read
In the ever-evolving landscape of programming languages, Java has consistently maintained its relevance by embracing modern programming paradigms while preserving its core principles. One such evolution is the introduction of the var keyword in Java 10. This innovative feature has sparked both excitement and debate within...
3 min read
In Java, JAR stands for Java ARchive, whose format is based on the zip format. The JAR files format is mainly used to aggregate a collection of files into a single one. It is a single cross-platform archive format that handles images, audio, and class files....
2 min read
In this section, we will learn how to multiply two numbers without using the arithmetic operator (*) in Java. The multiplication of two numbers can be found by the repeated addition method. It means that add the number (multiplicand) into itself up to multiplicator times. The method...
3 min read
The java.io.ObjectInputStream class is used to deserialize objects and primitive data iously serialized using the ObjectOutputStream. It allows the reconstruction of object graphs and ensures compatibility between the serialized object's class and the current JVM (Java Virtual Machine) class definitions. ObjectOutputStream and ObjectInputStream collaborate to save and...
22 min read
The bitwise complement operator falls under the category of the unary operator (deals with just a single operand). It takes one number and reverses all pieces of it. When a bitwise administrator is applied on bits, then, at that point, all the 1's turned into 0's...
3 min read
In this section, we will learn about the Morris traversal for preorder in Java. In Morris traversal, we do the traversal of a tree without the help of recursion or stack. The Morris traversal is based on the threaded binary tree. Morris Traversal Preorder Algorithm The following is...
4 min read
Initially, there are many methods and logic to find the first non-repeating character in a string, and it just needs implementation. For implementation, we need to understand the logic, and we need to have a full grip on the programming language. Before implementing the logic using...
7 min read
In the realm of Java programming, threads play a pivotal role in enabling concurrent execution of tasks. Threads are lightweight subprocesses that can run concurrently within a single process. Java offers two types of threads: user threads and daemon threads. Each type serves a distinct purpose...
6 min read
In this section, we will discuss how to print the matrix in Z form. Also, we will create a Java program that prints the all the Z elements of the matrix. The Z form includes the elements of the first row, right diagonal, and the last row...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India