Should we design a class and name methods encode and decode for completing this question? My solutions isnt getting picked up when i run code


  • 1
    S
    public class Codec {
        String alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        HashMap<String, String> map = new HashMap<>();
        Random rand = new Random();
        String key = getRand();
        public String getRand() {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 6; i++) {
                sb.append(alphabet.charAt(rand.nextInt(62)));
            }
            return sb.toString();
        }
    
        public String encode(String longUrl) {
            while (map.containsKey(key)) {
                key = getRand();
            }
            map.put(key, longUrl);
            return "http://tinyurl.com/" + key;
        }
    
        public String decode(String shortUrl) {
            return map.get(shortUrl.replace("http://tinyurl.com/", ""));
        }
    }
    
    class Main {
        public static void main(String[] args) {     
            Codec codec = new Codec();
            String res = codec.encode("https://leetcode.com/problems/design-tinyurl");
            codec.decode(res);        
        }
    }
    
    

  • 0
    V

    Hi @shiva2,

    This appears to be a design problem ; not sure how it is evaluated, though.

    Here is the companion coding problem: https://leetcode.com/problems/encode-and-decode-tinyurl/description/

    Cheers!


Log in to reply
 

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