The characters don’t need to be in particular order. For example, the characters [“y”, “r”, “o”, “u”] are needed to form the words [“your”, “you”, “or”, “yo”] Note: the input words won’t contain any spaces; however, they might contain punctuation and/or special characters. Sample Input words = ["this", "that", "did", "deed", "them!", "a"] Sample Output ["t", "t", "h", "i", "s", "a", "d", "d", "e", "e", "m", "!"] Solution Instruction ● You can submit your solution in the programming language of your choice. Y
SOLUTION TO THE ABOVE QUESTION
SOLUTION CODE
package com.company;
import java.util.ArrayList;
import java.util.*;
class Main {
public static void main(String arg[]) {
//propmt the user to enter the number words
Scanner sc = new Scanner(System.in);
System.out.print("\nEnter the number of words you want to input: ");
int number_of_words = sc.nextInt();
//lets create an array list of charcters
ArrayList<Character> my_characters = new ArrayList<Character>();
for(int i = 0; i < number_of_words; i++)
{
System.out.print("Enter a string "+(i+1)+": ");
String my_string = sc.next();
for(int j = 0; j < my_string.length(); j++)
{
if(i==0)
{
my_characters.add(my_string.charAt(j));
}
else
{
for(int n = 0; n < my_string.length(); n++)
{
//let us count the charcter at position n is occuring how many
// times in the my_charcters_array list
int char_array_list_frequency = 0;
for(int m = 0; m < my_characters.size(); m++)
{
if(my_characters.get(m).equals(my_string.charAt(n)))
{
char_array_list_frequency = char_array_list_frequency + 1;
}
}
//let us check how many characters at index n, the string conatins
int char_my_string_frequency = 0;
for(int k = 0; k < my_string.length(); k++)
{
if(Objects.equals(my_string.charAt(k), my_string.charAt(n)))
{
char_my_string_frequency = char_my_string_frequency + 1;
}
}
//let us check if we have to add the charcter in our characters array list
//let us get the diffrerence of the ocurences
int difference = char_my_string_frequency-char_array_list_frequency;
//ad the charcter to the array list
for(int r = 0; r < difference; r++)
{
my_characters.add(my_string.charAt(n));
}
}
}
}
}
//let us print the output
System.out.print("\nOUTPUT: "+my_characters+"\n");
}
}
SAMPLE PROGRAM OUTPUT
Comments
Leave a comment