A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Amazon asked Java IQ: There is a dictionary already implemented. Write a method , which takes input String without space, to replace the characters from the strings which are not present in dictionary with _

There is a dictionary already implemented. Write a method , which takes input String without space, to replace the characters from the strings which are not present in dictionary with _

Amazon IQ

2. There is a dictionary already implemented. Write a method , which takes input String without space, to replace the characters from the strings which are not present in dictionary with _. Example: Dictionary – a* Input- aaabaa Output- aaa_aa

import java.util.Hashtable;

public class Dictionary_aStar {
    
     public static void main(String[] args)
    {
           Hashtable<String, Integer>  dict = new Hashtable<>();            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++)
            {
            System.out.println(input.substring(i, j-i+1).replaceAll("b", "_"));
               
            }
        }
    }

}


Output:
a
aa
aaa
aaa_
aaa_a
aaa_aa

a
aa
aa_

aa_a











LEARN TUTORIALS

.

.