8 lines simple Java


  • 0
    S
    for(int i=0;i<lena;i++){
                if(mapb.put(pattern.charAt(i)+"",i)!=mapb.put(arr[i],i)) return false;
            }
    

    Hi,

    when I use int instead of Integer in the loop 31st test case is not passing. Can you please let me know the error. When I change it to Integer the test case is passsing. Can you please what difference is "int" and "Integer" making.


  • 0
    S

    Hi,

    Can you please let me know the default <Key, Value> pair datatypes when teh hashmap is declared likewise in your code.

    Map index = new HashMap();

    Thanks


  • 0
    X

    @sgandham I think they should be <Object, Object>


  • 0
    F

    Hi Stefan, Thanks for your always-nice solution. I think the reason you come up with this solution is because you want to save the space comparing to the two-hashmap method. However, have you ever thought about how much space will a new object of class Object hold? I searched online but no standard result. I mean, if the object consume much more space than a String object or a Character object, then maybe your solution cannot improve the space complexity. Hope your explanation. Thanks.


  • 0

    @FelixGEEK No, the reasons I use one map instead of two are that (1) it's less code and (2) it might be confusing/interesting/educational for some people :-). And no, I don't know about these space details. But I don't see why the objects would take more or less space than with two maps. The exact same objects would be stored, no?


  • 0
    F

    @StefanPochmann Hi Stefan. Okay, I see. From the perspective of less and interesting code, your solution is great. For the space detail, I think a map<Object, Object> should hold more space than a map<Integer, Integer>. Since the machine doesn't know what will be stored into the map, it must preserve more space for the largest possible key and value. For example, if is a map<Character, Character>, the space may only be 2 bytes for the Character key, but when the map is <Object, Object>, the space may be at least 8 bytes for the Object key, since you may put a Long value as a key.. That's my opinion on the space, correct me if I'm ridiculous. Thanks!


  • 0

    @FelixGEEK I don't know how it's done and I guess it might depend on the implementation, but I suspect that the map just stores references to the already existing objects, so it shouldn't make a difference. I just tried this:

    import java.util.*;
    
    class Test {
        public static void main(String[] args) {
            //Object[] objects = new Object[100_000_000];
            //Character[] chars = new Character[100_000_000];
            String[] strings = new String[100_000_000];
    
            System.out.println("done");
            while (true);
        }
    }
    

    And then the Windows Task Manager showed that the process took 392 MB, regardless of whether I used Object, Character or String. Which is 32 bits per entry. And apparently references typically do have 32 bits: https://stackoverflow.com/a/258150/1672429


  • 0
    F

    @StefanPochmann Okay. I see. Thanks for the experiments and the link.


Log in to reply
 

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