Amazon Interview Question
There is a dictionary already implemented. Write a method, which takes input String without space, to prints all subsets of the input string which is present in dictionary.
//import java.util.Dictionary;
import java.util.Hashtable;
/*
1. There is a dictionary already implemented. Write a method, which takes input String without space,
to prints all subsets of the input string which is present in dictionary.
Example: Dictionary – a*
Input- aaabaa
Output- a,a,a,aa,aa,aaa,a,a,aa
*/
public class Dictionary_aStar {
public static void main(String[] args)
{
//Dictionary<String, Integer> dict = new Dictionary<>(); Dictionary is abstract class.
Hashtable<String, Integer> dict = new Hashtable<>(); //Hashtable extends Dictionary class.
dict.put("a", 1);
dict.put("a", 1);
dict.put("aa", 1);
dict.put("aaa", 1);
dict.put("aaaa", 1);
String input = "aaabaa";
get_combinations(input, dict);
}
public static void get_combinations(String input, Hashtable<String, Integer> dict)
{
for (int i = 0; i < input.length(); i++)
{
for (int j = i ; j < input.length(); j++)
{
if (dict.containsKey(input.substring(i, j-i+1)))
{
System.out.println(input.substring(i, j-i+1));
}
}
}
}
}