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

Summarize

Git is a distributed version control system DVCS designed for efficient source code management, suitable for both small and large projects. It allows multiple developers to work on a project simultaneously without overwriting changes, supporting collaborative work, continuous integration, and deployment. This Git and GitHub tutorial is designed for beginners to learn fundamentals and advanced concepts, including branching, pushing, merging conflicts, and essential Git commands. Prerequisites include familiarity with the command line interface CLI, a text editor, and basic programming concepts. Git was developed by Linus Torvalds for Linux kernel development and tracks changes, manages versions, and enables collaboration among developers. It provides a complete backup of project history in a repository. GitHub is a hosting service for Git repositories, facilitating project access, collaboration, and version control. The tutorial covers topics such as Git installation, repository creation, Git Bash usage, managing branches, resolving conflicts, and working with platforms like Bitbucket and GitHub. The text is a comprehensive guide to using Git and GitHub, covering a wide range of topics. It includes instructions on working directories, using submodules, writing good commit messages, deleting local repositories, and understanding Git workflows like Git Flow versus GitHub Flow. There are sections on packfiles, garbage collection, and the differences between concepts like HEAD, working tree, and index. Installation instructions for Git across various platforms Ubuntu, macOS, Windows, Raspberry Pi, Termux, etc. are provided, along with credential setup. The guide explains essential Git commands, their usage, and advanced topics like debugging, merging, rebasing, patch operations, hooks, subtree, filtering commit history, and handling merge conflicts. It also covers managing branches, syncing forks, searching errors, and differences between various Git operations e.g., push origin vs. push origin master, merging vs. rebasing. The text provides a comprehensive guide on using Git and GitHub. It covers creating repositories, adding code of conduct, forking and cloning projects, and adding various media files to a repository. The text explains how to push projects, handle authentication issues, solve common Git problems, and manage repositories. It discusses using different IDEs like VSCode, Android Studio, and PyCharm, for Git operations, including creating branches and pull requests. Additionally, it details deploying applications to platforms like Heroku and Firebase, publishing static websites on GitHub Pages, and collaborating on GitHub. Other topics include the use of Git with R and Eclipse, configuring OAuth apps, generating personal access tokens, and setting up GitLab repositories. The text covers various topics related to Git, GitHub, and other version control systems Key Pointers Git is a distributed version control system DVCS for source code management. Supports collaboration, continuous integration, and deployment. Suitable for both small and large projects. Developed by Linus Torvalds for Linux kernel development. Tracks changes, manages versions, and provides complete project history. GitHub is a hosting service for Git repositories. Tutorial covers Git and GitHub fundamentals and advanced concepts. Includes instructions on installation, repository creation, and Git Bash usage. Explains managing branches, resolving conflicts, and using platforms like Bitbucket and GitHub. Covers working directories, submodules, commit messages, and Git workflows. Details packfiles, garbage collection, and Git concepts HEAD, working tree, index. Provides Git installation instructions for various platforms. Explains essential Git commands and advanced topics debugging, merging, rebasing. Covers branch management, syncing forks, and differences between Git operations. Discusses using different IDEs for Git operations and deploying applications. Details using Git with R, Eclipse, and setting up GitLab repositories. Explains CI/CD processes and using GitHub Actions. Covers internal workings of Git and its decentralized model. Highlights differences between Git version control system and GitHub hosting platform.

2 trials left

 

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.

You may also like this!