Simple Java Solution Using HashMap


  • 0
    S

    public class LogSystem {
    HashMap<Integer,String> hm;
    HashMap<String,Integer> gran=new HashMap<>();

    public LogSystem() {
        hm=new HashMap<Integer,String>();
        gran.put("Year",4);
        gran.put("Month",7);
        gran.put("Day",10);
        gran.put("Hour",13);
        gran.put("Minute",16);
        gran.put("Second",19);
    }
    
    public void put(int id, String timestamp) {
        hm.put(id,timestamp);
    }
    
    public List<Integer> retrieve(String s, String e, String gra) {
        List<Integer> res=new ArrayList<Integer>();
        int index=gran.get(gra);
        s=s.substring(0,index);
        e=e.substring(0,index);
        for(Map.Entry<Integer,String> me:hm.entrySet()){
            String a=me.getValue();
            a=a.substring(0,index);
            if(a.compareTo(s)>=0 && a.compareTo(e)<=0) res.add(me.getKey());
        }
        return res;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.