Untitled
unknown
plain_text
3 years ago
1.3 kB
4
Indexable
package ee.ttu.algoritmid.bond; import java.util.HashMap; import java.util.Map; import java.util.Objects; public class DisjointSubsets { private Map<String, String> hashMap = new HashMap<>(); public String find(String element) throws IllegalArgumentException { if (Objects.equals(hashMap.get(element), element)) { return element; } // should throw IllegalArgumentException if any of elements is not present if (!hashMap.containsKey(element)) throw new IllegalArgumentException(); return find(hashMap.get(element)); } public void union(String element1, String element2) throws IllegalArgumentException { // should throw IllegalArgumentException if any of elements is not present var a = find(element1); if (a == null) throw new IllegalArgumentException(); var b = find(element2); if (b == null) throw new IllegalArgumentException(); hashMap.put(a, b); } public void addSubset(String element) throws IllegalArgumentException { // should throw IllegalArgumentException if any of elements is not present if (hashMap.containsKey(element)) throw new IllegalArgumentException(); hashMap.put(element, element); } }
Editor is loading...