Print list items containing all characters of a given word
Last Updated :
10 Mar, 2023
There is a list of items. Given a specific word, e.g., "sun", print out all the items in list which contain all the characters of "sun".
For example if the given word is "sun" and the items are "sunday", "geeksforgeeks", "utensils", ""just" and "sss", then the program should print "sunday" and "utensils".
Algorithm: Thanks to geek4u for suggesting this algorithm.
1) Initialize a binary map:
map[256] = {0, 0, ..}
2) Set values in map[] for the given word "sun"
map['s'] = 1, map['u'] = 1, map['n'] = 1
3) Store length of the word "sun":
len = 3 for "sun"
4) Pick words (or items)one by one from the list
a) set count = 0;
b) For each character ch of the picked word
if(map['ch'] is set)
increment count and unset map['ch']
c) If count becomes equal to len (3 for "sun"),
print the currently picked word.
d) Set values in map[] for next list item
map['s'] = 1, map['u'] = 1, map['n'] = 1
C++
// C++ program to print all strings that contain all
// characters of a word
#include <bits/stdc++.h>
#include<stdio.h>
#include<string.h>
using namespace std;
# define NO_OF_CHARS 256
/* prints list items having all characters of word */
void print(char list[][50], char *word, int list_size)
{
/*Since calloc is used, map[] is initialized as 0 */
int *map = new int[(sizeof(int)*NO_OF_CHARS)];
int i, j, count, word_size;
/*Set the values in map */
for (i = 0; *(word+i); i++)
map[*(word + i)] = 1;
/* Get the length of given word */
word_size = strlen(word);
/* Check each item of list if has all characters
of word*/
for (i = 0; i < list_size; i++)
{
for (j = 0, count = 0; *(list[i] + j); j++)
{
if (map[*(list[i] + j)])
{
count++;
/* unset the bit so that strings like
sss not printed*/
map[*(list[i] + j)] = 0;
}
}
if (count == word_size)
cout << list[i] << endl;
/*Set the values in map for next item*/
for (j = 0; *(word + j); j++)
map[*(word + j)] = 1;
}
}
/* Driver code*/
int main()
{
char str[] = "sun";
char list[][50] = {"geeksforgeeks", "unsorted", "sunday",
"just", "sss" };
print(list, str, 5);
return 0;
}
// This is code is contributed by rathbhupendra
C
// C program to print all strings that contain all
// characters of a word
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define NO_OF_CHARS 256
/* prints list items having all characters of word */
void print(char *list[], char *word, int list_size)
{
/*Since calloc is used, map[] is initialized as 0 */
int *map = (int *)calloc(sizeof(int), NO_OF_CHARS);
int i, j, count, word_size;
/*Set the values in map */
for (i = 0; *(word+i); i++)
map[*(word + i)] = 1;
/* Get the length of given word */
word_size = strlen(word);
/* Check each item of list if has all characters
of word*/
for (i = 0; i < list_size; i++)
{
for (j = 0, count = 0; *(list[i] + j); j++)
{
if (map[*(list[i] + j)])
{
count++;
/* unset the bit so that strings like
sss not printed*/
map[*(list[i] + j)] = 0;
}
}
if (count == word_size)
printf("\n %s", list[i]);
/*Set the values in map for next item*/
for (j = 0; *(word+j); j++)
map[*(word + j)] = 1;
}
}
/* Driver program to test to print printDups*/
int main()
{
char str[] = "sun";
char *list[] = {"geeksforgeeks", "unsorted", "sunday",
"just", "sss" };
print(list, str, 5);
getchar();
return 0;
}
Java
// Java program to print all strings that contain all
// characters of a word
class GFG
{
static final int NO_OF_CHARS = 256;
/* prints list items having all characters of word */
static void print(String[] list, String word, int list_size)
{
/*
* Since calloc is used, map[] is initialized as 0
*/
int[] map = new int[NO_OF_CHARS];
int i, j, count, word_size;
/* Set the values in map */
for (i = 0; i < word.length(); i++)
map[word.charAt(i)] = 1;
/* Get the length() of given word */
word_size = word.length();
/*
* Check each item of list if has all characters of word
*/
for (i = 0; i < list_size; i++)
{
for (j = 0, count = 0; j < list[i].length(); j++)
{
if (map[list[i].charAt(j)] > 0)
{
count++;
/*
* unset the bit so that strings like sss not printed
*/
map[list[i].charAt(j)] = 0;
}
}
if (count == word_size)
System.out.println(list[i]);
/* Set the values in map for next item */
for (j = 0; j < word.length(); j++)
map[word.charAt(j)] = 1;
}
}
/* Driver code */
public static void main(String[] args)
{
String str = "sun";
String[] list = { "geeksforgeeks", "unsorted",
"sunday", "just", "sss" };
print(list, str, 5);
}
}
// This code is contributed by sanjeev2552
Python
# Python program to print the list items containing all
# characters of a given word
NO_OF_CHARS = 256
# Prints list items having all characters of word
def printList(list, word, list_size):
map = [0] * NO_OF_CHARS
# Set the values in map
for i in word:
map[ord(i)] = 1
# Get the length of given word
word_size = len(word)
# Check each item of list if has all characters
# of words
for i in list:
count = 0
for j in i:
if map[ord(j)]:
count+=1
# unset the bit so that strings like sss
# not printed
map[ord(j)] = 0
if count==word_size:
print i
# Set the values in map for next item
for j in xrange(len(word)):
map[ord(word[j])] = 1
# Driver program to test the above function
string = "sun"
list = ["geeksforgeeks", "unsorted", "sunday", "just", "sss"]
printList(list, string, 5)
# This code is contributed by Bhavya Jain
C#
// C# program to print all strings that contain
// all characters of a word
using System;
class GFG{
static int NO_OF_CHARS = 256;
// Prints list items having all characters of word
static void print(string[] list, string word,
int list_size)
{
// Since calloc is used, map[] is
// initialized as 0
int[] map = new int[NO_OF_CHARS];
int i, j, count, word_size;
// Set the values in map
for (i = 0; i < word.Length; i++)
map[word[i]] = 1;
// Get the length() of given word
word_size = word.Length;
// Check each item of list if has all
// characters of word
for(i = 0; i < list_size; i++)
{
for(j = 0, count = 0; j < list[i].Length; j++)
{
if (map[list[i][j]] > 0)
{
count++;
// unset the bit so that strings like
// sss not printed
map[list[i][j]] = 0;
}
}
if (count == word_size)
Console.WriteLine(list[i]);
// Set the values in map for next item
for(j = 0; j < word.Length; j++)
map[word[j]] = 1;
}
}
// Driver code
public static void Main(string[] args)
{
string str = "sun";
string[] list = { "geeksforgeeks", "unsorted",
"sunday", "just", "sss" };
print(list, str, 5);
}
}
// This code is contributed by ukasp
JavaScript
// Javascript ptogram for the above approach
const NO_OF_CHARS = 256;
function printList(list, word, list_size) {
const map = new Array(NO_OF_CHARS).fill(0);
// Set the values in map
for (let i = 0; i < word.length; i++) {
map[word.charCodeAt(i)] = 1;
}
// Get the length of given word
const word_size = word.length;
// Check each item of list if has all characters
// of words
for (let i = 0; i < list.length; i++) {
let count = 0;
for (let j = 0; j < list[i].length; j++) {
if (map[list[i].charCodeAt(j)]) {
count++;
// unset the bit so that strings like sss
// not printed
map[list[i].charCodeAt(j)] = 0;
}
}
if (count == word_size) {
console.log(list[i]);
}
// Set the values in map for next item
for (let j = 0; j < word.length; j++) {
map[word.charCodeAt(j)] = 1;
}
}
}
const string = "sun";
const list = ["geeksforgeeks", "unsorted", "sunday", "just", "sss"];
printList(list, string, 5);
// This code is contributed by codebraxnzt
Output:
unsorted
sunday
Time Complexity: O(n + m) where n is total number of characters in the list of items. And m = (number of items in list) * (number of characters in the given word)
Space Complexity: O(n)
Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem
Similar Reads
Possible Words using given characters in Python Given a dictionary and a character array, print all valid words that are possible using characters from the array. Note: Repetitions of characters is not allowed. Examples: Input : Dict = ["go","bat","me","eat","goal","boy", "run"] arr = ['e','o','b', 'a','m','g', 'l'] Output : go, me, goal. This pr
5 min read
Missing characters to make a string Pangram Pangram is a sentence containing every letter in the English alphabet. Given a string, find all characters that are missing from the string, i.e., the characters that can make the string a Pangram. We need to print output in alphabetic order. Examples: Input : welcome to geeksforgeeksOutput : abdhij
8 min read
Count words in a given string Given a string, count the number of words in it. The words are separated by the following characters: space (' ') or new line ('\n') or tab ('\t') or a combination of these. Recommended PracticeCount number of wordsTry It!Method 1: The idea is to maintain two states: IN and OUT. The state OUT indica
15+ min read
Print all valid words from given dictionary that are possible using Characters of given Array Given a dictionary of strings dict[] and a character array arr[]. The task is to print all valid words of the dictionary that are possible using characters from the given character array.Examples:Input: dict[] = ["go", "bat", "me", "eat", "goal", boy", "run"] , arr[] = ['e', 'o', 'b', 'a', 'm', 'g',
7 min read
String containing first letter of every word in a given string with spaces String str is given which contains lowercase English letters and spaces. It may contain multiple spaces. Get the first letter of every word and return the result as a string. The result should not contain any space. Examples: Input : str = "geeks for geeks" Output : gfg Input : str = "geeks for geek
11 min read