Answer to Question #268542 in Java | JSP | JSF for Abhishek

Question #268542

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


1
Expert's answer
2021-11-19T17:14:33-0500


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







Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS