Java Code Using HashMap


  • 2
    K
    public class LogSystem {
        Map<Integer,String> map=new HashMap<>();
        public LogSystem() {
            
        }
        
        public void put(int id, String timestamp) {
            map.put(id,timestamp);
        }
        
        public List<Integer> retrieve(String s, String e, String gra) {
            int x=0;
            switch (gra){
                case "Year":
                    x=4; 
                    break;
                case "Month":
                    x=7;
                    break;
                case "Day":
                    x=10;
                    break;
                case "Hour":
                    x=13;
                    break;
                case "Minute":
                    x=16;
                    break;
                case "Second":
                    x=19;
                    break;
            }
            s=s.substring(0,x);
            e=e.substring(0,x);
            List<Integer> ans=new ArrayList<>();
            for (Integer i:map.keySet())
            {
                String ss=map.get(i).substring(0,x);
                if (ss.compareTo(s)>=0 && ss.compareTo(e)<=0) ans.add(i);
            }
            return ans;
        }
    }
    
    /**
     * Your LogSystem object will be instantiated and called as such:
     * LogSystem obj = new LogSystem();
     * obj.put(id,timestamp);
     * List<Integer> param_2 = obj.retrieve(s,e,gra);
     */
    

  • 0
    R

    @KakaHiguain You nailed it!


  • 0
    F

    awesome solution! I ve been thinking for a while about how to compare.


  • 0
    R

    @KakaHiguain Awesome solution


  • 0
    Y

    Same idea, but using stream:

    public class LogSystem {
        private Map<Integer, String> map;
    
        public LogSystem() {
            map = new HashMap<>();
        }
    
        public void put(int id, String timestamp) {
            map.put(id, timestamp);
        }
    
        public List<Integer> retrieve(String s, String e, String gra) {
            return map.entrySet().stream().filter(entry -> {
                switch (gra) {
                    case "Year":
                        return s.substring(0, 4).compareTo(entry.getValue().substring(0, 4)) <= 0 &&
                                e.substring(0, 4).compareTo(entry.getValue().substring(0, 4)) >= 0;
                    case "Month":
                        return s.substring(0, 7).compareTo(entry.getValue().substring(0, 7)) <= 0 &&
                                e.substring(0, 7).compareTo(entry.getValue().substring(0, 7)) >= 0;
                    case "Day":
                        return s.substring(0, 10).compareTo(entry.getValue().substring(0, 10)) <= 0 &&
                                e.substring(0, 10).compareTo(entry.getValue().substring(0, 10)) >= 0;
                    case "Hour":
                        return s.substring(0, 13).compareTo(entry.getValue().substring(0, 13)) <= 0 &&
                                e.substring(0, 13).compareTo(entry.getValue().substring(0, 13)) >= 0;
                    case "Minute":
                        return s.substring(0, 16).compareTo(entry.getValue().substring(0, 16)) <= 0 &&
                                e.substring(0, 16).compareTo(entry.getValue().substring(0, 16)) >= 0;
                    case "Second":
                        return s.substring(0, 19).compareTo(entry.getValue().substring(0, 19)) <= 0 &&
                                e.substring(0, 19).compareTo(entry.getValue().substring(0, 19)) >= 0;
                    default:
                        return false;
                }
            }).map(Map.Entry::getKey).collect(Collectors.toList());
        }
    }
    

Log in to reply
 

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