Posts

Showing posts from September, 2023

Shortest Substring hackerrank

import java.io.*; import java.lang.*; import java.util.*;   class sm{   static int smallestSubstring(String a) {           // Stores all occurrences     ArrayList a1 = new ArrayList ();       // Generate all the substrings     for(int i = 0; i a2 = new TreeMap ();     for(String s : a1)         a2.put(s, a2.getOrDefault(s, 0) + 1);       ArrayList freshlist = new ArrayList ();       // Iterate over all     // unique substrings     for(String s : a2.keySet())     {                   // If frequency is 1         if (a2.get(s) == 1)               // Append into fresh list             freshlist.add(s);     }       // Initialize a dictionary     TreeMap dictionary = new TreeMap ();       for(String s : freshlist)     {                   // Append the keys         dictionary.put(s, s.length());     }       ArrayList newlist = new ArrayList ();       // Traverse the dictionary     for(String s : dictionary.keySet())         newlist.add(dictionary.get(s));       int ans = Integer.MAX_VAL...