w3resource

Java: Print list items containing all characters of a specified word


42. List Items Containing Word Letters

Write a Java program to print a list of items containing all characters of a given word.

Visual Presentation:

Java String Exercises: Print list items containing all characters of a specified word


Sample Solution:

Java Code:

// Importing necessary Java utilities.
import java.util.*;

// Define a class named Main.
class Main {
    
    // Define a method to check if a given string contains all the letters of another string.
    static void checkExistance(String str1, String str_to_search) {
        int chk = 0; // Variable to check existence.
        char chhr = ' '; // Character variable.
        int[] a = new int[Character.MAX_VALUE + 1]; // Array to store character counts.

        // Loop through each character of the first string and count occurrences.
        for (int i = 0; i < str1.length(); i++) {
            chhr = str1.charAt(i);
            ++a[chhr];
        }
        
        // Loop through each character of the string to search.
        for (int i = 0; i < str_to_search.length(); i++) {
            chhr = str_to_search.charAt(i);
            if (a[chhr] >= 1)
                chk = 1; // Set check flag if character exists in the first string.
        }
        
        // If check flag is set, print the first string.
        if (chk == 1)
            System.out.println(str1);
    }

    // Main method to execute the program.
    public static void main(String[] args) {
        // Create a list of strings.
        List < String > list = new ArrayList < String > ();
        list.add("rabbit");
        list.add("bribe");
        list.add("dog");

        // Print the given strings.
        System.out.print("The given strings are: ");
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + "   ");
        }
        
        // Print the given word.
        System.out.println("\nThe given word is: bib ");
        System.out.println("\nThe strings containing all the letters of the given word are: ");
        
        // Check each string in the list for the existence of letters from the given word.
        for (int j = 0; j < list.size(); j++) {
            checkExistance(list.get(j), "bib");
        }
    }
}

Sample Output:

The given strings are: rabbit   bribe   dog   
The given word is: bib 

The strings containing all the letters of the given word are: 
rabbit
bribe

Flowchart:

Flowchart: Java String Exercises - Print list items containing all characters of a specified word



For more Practice: Solve these Related Problems:

  • Write a Java program to filter a list of words and display only those that contain all characters of a given pattern.
  • Write a Java program to check a list of strings and output those that include every character from a specified keyword.
  • Write a Java program to iterate over an array of strings and print only the strings that fully contain a target set of characters.
  • Write a Java program to read a series of words and display the ones that contain a complete set of letters from another word.

Go to:


PREV : Remove Duplicates Using Mask String.
NEXT : Most Frequent Character.

Java Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.