How to Print List Items Containing All Characters of a Given Word


 

In programming, there often arises the need to search through a list of items and find those that contain all the characters of a given word. This task can be efficiently accomplished using various programming languages such as C++, C, Java & Python. In this article, we will explore algorithms and provide code implementations in each of these languages, along with their respective outputs and complexities.

Algorithm:

The algorithm employed revolves around iterating through each item in the list and, for each item, traversing through each character of the given word. By checking if the current item contains all characters of the word, the desired items are identified and printed.

  1. Iterate through each item in the list.
  2. For each item, iterate through each character in the given word.
  3. Check if the current item contains all the characters of the given word.
  4. If it does, print the item.
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void printItemsContainingWord(vector<string> items, string word) {
    for (string item : items) {
        bool containsAllChars = true;
        for (char c : word) {
            if (item.find(c) == string::npos) {
                containsAllChars = false;
                break;
            }
        }
        if (containsAllChars) {
            cout << item << endl;
        }
    }
}

int main() {
    vector<string> items = {"apple", "banana", "orange", "grape"};
    string word = "an";
    printItemsContainingWord(items, word);
    return 0;
}

Output

banana
orange
#include <stdio.h>
#include <string.h>

void printItemsContainingWord(char items[][20], int size, char* word) {
    for (int i = 0; i < size; i++) {
        int containsAllChars = 1;
        for (int j = 0; j < strlen(word); j++) {
            if (strchr(items[i], word[j]) == NULL) {
                containsAllChars = 0;
                break;
            }
        }
        if (containsAllChars) {
            printf("%s\n", items[i]);
        }
    }
}

int main() {
    char items[][20] = {"apple", "banana", "orange", "grape"};
    char word[] = "an";
    printItemsContainingWord(items, 4, word);
    return 0;
}

Output

banana
orange
def print_items_containing_word(items, word):
    for item in items:
        if all(c in item for c in word):
            print(item)

items = ["apple", "banana", "orange", "grape"]
word = "an"
print_items_containing_word(items, word)

Output

banana
orange
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void printItemsContainingWord(List<String> items, String word) {
        for (String item : items) {
            boolean containsAllChars = true;
            for (char c : word.toCharArray()) {
                if (item.indexOf(c) == -1) {
                    containsAllChars = false;
                    break;
                }
            }
            if (containsAllChars) {
                System.out.println(item);
            }
        }
    }

    public static void main(String[] args) {
        List<String> items = new ArrayList<>();
        items.add("apple");
        items.add("banana");
        items.add("orange");
        items.add("grape");
        String word = "an";
        printItemsContainingWord(items, word);
    }
}

Output

banana
orange

Time Complexity:

The time complexity of the algorithm is O(n*m), where 'n' represents the number of items in the list and 'm' signifies the length of the given word. This complexity arises due to the nested iteration process required to search through both the list and the word.

Space Complexity:

Regarding space complexity, the algorithm operates efficiently with an O(1) complexity. The amount of additional space utilized remains constant, independent of the input size, as only a minimal amount of space is required for variables such as loop counters and temporary storage.

Conclusion:

By dissecting the intricacies of searching for list items containing all characters of a given word, this article equips you with invaluable insights and practical implementations across multiple programming languages. Understanding the nuances of time and space complexities enables you to optimize your code and navigate search operations effectively in your programming endeavors.

       

Advertisements

ads