Find the closest anagram of a given string.
Similar flavor of problems are also defined in LeetCode A collection of hundreds of interview questions and solutions are available in our blog at Interview Question
Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Map; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
public class AnagramCloseMaker { | |
public static List<String> anagramProc(String[] str) { | |
//create a hash table and store the sorted version of word as key and the anagrams as values in list | |
Map<String, List<String>> mapAna = new HashMap<String, List<String>>(); | |
//sort the string iteratively | |
for (String s : str) { | |
String temp = s; | |
Arrays.sort(temp.toCharArray()); | |
if (!mapAna.containsKey(temp)) { | |
List<String> anaList = new ArrayList<String>(); | |
anaList.add(s); | |
mapAna.put(temp, anaList); | |
} else { | |
mapAna.get(temp).add(s); | |
} | |
} | |
//now add all the anagrams from the hash to a list and return it | |
List<String> anagrams = new ArrayList<String>(); | |
for (String ana : mapAna.keySet()) { | |
anagrams.addAll(mapAna.get(ana)); | |
} | |
return anagrams; | |
} | |
public static void main(String args[]) { | |
String[] s = {"aba", "def", "baa", "fed", "cow", "owc"}; | |
List<String> anaList = anagramProc(s); | |
for(String ss : anaList) { | |
System.out.println(ss); | |
} | |
} | |
} |
No comments:
Post a Comment