Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here my code : My question is HOW do i remove the duplicates in my final answer. import java.io.*; import java.util.*; public class Main {

Here my code :

My question is HOW do i remove the duplicates in my final answer.

import java.io.*;

import java.util.*;

public class Main {

//List for Collecting permutations of word

static ArrayList perms;

//This method calculates the permutations of the string using recursion and stores in the above list.

static void genPerms(char str[], int ind){

if(ind==str.length-1){

perms.add(String.valueOf(str));

return;

}

for(int i=ind;i

char ch = str[i];

str[i] = str[ind];

str[ind] = ch;

genPerms(str, ind+1);

ch = str[i];

str[i] = str[ind];

str[ind] = ch;

}

}

public static void main(String[] args) throws IOException {

//Reading the dictionary into a set

String dict = args[0];

BufferedReader f1 = new BufferedReader(new FileReader(new File(dict)));

String word;

HashSet hs = new HashSet();

while((word = f1.readLine())!=null){

hs.add(word);

}

//Reading the words

String jumbles = args[1];

BufferedReader f2 = new BufferedReader(new FileReader(new File(jumbles)));

//List for storing final answer

ArrayList fans = new ArrayList();

//Iterating through all the jumble words

while((word = f2.readLine())!=null){

//finding permutations

perms = new ArrayList();

genPerms(word.toCharArray(),0);

String ans = word;

//Storing perms in a List

ArrayList bunch = new ArrayList();

for(String x : perms){

if(hs.contains(x)){

bunch.add(x);

}

}

//Sorting the list

Collections.sort(bunch);

//Calculating the answer

for(String x : bunch){

ans = ans+" "+x;

}

fans.add(ans);

}

//Sorting the answer vertically

Collections.sort(fans);

for(String x : fans){

System.out.println(x);

}

}

}

tinyDictionary.txt

act cat tac dog god post pots stop spot tops opts rat tar art trap tarp part flog golf frog gorp glossy 

tinyJumbles.txt

atc otsp gdo atr arpt grof sylogs 

tinyCorrectOuput.txt.

arpt part tarp trap atc act cat tac atr art rat tar gdo dog god grof frog otsp opts post pots spot stop tops sylogs glossy 

Current OUTPUT :

arpt part tarp trap atc act cat tac atr art rat tar gdo dog god grof frog otsp opts post pots spot stop tops sylogs glossy glossy

NeverMind Just Figured it out LOL it was so easy :)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions