Given a string str, consisting of non-alphabetical characters. The task is to remove all those non-alphabetical characters of str and print the words on a new line.
Examples:
Input: str = “Hello, how are you ?”
Output: Hello how are you comma(, ), white space and question mark (?) are removed and there are total 4 words in string s. Each token is printed in the same order in which it appears in string s.Input: “Azad is a good boy, isn’ t he ?”
Output: Azad is a good boy isn t he
Approach:
Non-alphabetic characters are basically any character that is not a number or letter. It can be English alphabetic letters, blank spaces, exclamation points (!), commas (, ), question marks (?), periods (.), underscores (_), apostrophes (‘), and at symbols (@). The approach is to use Java String.split method to split the String, s into an array of substrings. Then print each n words on a new line in the same order as it appears in String s.
Below is the implementation of the above approach:
Java
// Java program to split all // non-alphabetical characters import java.util.Scanner; public class Main { // Function to trim the non-alphabetical characters static void printwords(String str) { // eliminate leading and trailing spaces str = str.trim(); // split all non-alphabetic characters String delims = "\\W+" ; // split any non word String[] tokens = str.split(delims); // print the tokens for (String item : tokens) { System.out.println(item + " " ); } } public static void main(String[] args) { String str = "Hello, how are you ?" ; printwords(str); } } |
Hello how are you
Time Complexity: O(N)
New Approach:
- Defines a function named removeNonAlphabetic which takes a string argument str as input and returns a string.
- The removeNonAlphabetic function uses the replaceAll method of the string class to replace all non-alphabetic characters in the input string with an empty string.
- The regular expression “[^a-zA-Z]” is used to match all characters that are not English alphabetical letters (both uppercase and lowercase).
- The main function of the code initializes a string str with a sample string “Hello, how are you?”.
- The main function then calls the removeNonAlphabetic function with str as input and assigns the returned string to a variable named result.
- Finally, the main function prints the value of the result string to the console, which should be the original string with all non-alphabetic characters removed.
Below is the implementation of the above approach:
Java
import java.util.Scanner; public class Main { // Function to remove non-alphabetic characters static String removeNonAlphabetic(String str) { // Use regular expression to match all non-alphabetic characters and replace with empty string String result = str.replaceAll( "[^a-zA-Z]" , "" ); return result; // Return the resulting string } public static void main(String[] args) { String str = "Hello, how are you ?" ; // Initialize a sample string String result = removeNonAlphabetic(str); // Call the removeNonAlphabetic function with input string System.out.println(result); // Print the resulting string with non-alphabetic characters removed } } |
Hellohowareyou
In this approach, we use the replaceAll() method to replace all non-alphabetic characters with an empty string. The regular expression “[^a-zA-Z]” matches any character that is not an English alphabetical letter (both uppercase and lowercase).
The resulting string only contains alphabetic characters.
Time Complexity:
- The replaceAll method in the removeNonAlphabetic function uses regular expressions to replace non-alphabetic characters with an empty string.
- The time complexity of regular expression matching depends on the size of the input string and the complexity of the regular expression used.
- In this case, the regular expression “[^a-zA-Z]” matches all non-alphabetic characters, and the replaceAll method replaces them with an empty string in a single pass.
- Therefore, the time complexity of the removeNonAlphabetic function is O(n), where n is the length of the input string.
- The main function only calls the removeNonAlphabetic function once, so its time complexity is also O(n).
Auxiliary Space:
- The space complexity of the removeNonAlphabetic function depends on the size of the input string and the size of the resulting string after removing non-alphabetic characters.
- In this case, the replaceAll method creates a new string that is the same size as the input string or smaller if non-alphabetic characters are removed.
- Therefore, the space complexity of the removeNonAlphabetic function is O(n), where n is the length of the input string.
The main function only stores a single string variable, so its space complexity is also O(n).