JAVA HASHMAP


  • 0

    class Solution {
    public int longestPalindrome(String s) {
    HashMap<Character,Integer> hp = new HashMap<Character,Integer>();
    char[] arr = s.toCharArray();
    for(int i=0;i< arr.length;i++){
    if(!hp.containsKey(arr[i])){
    hp.put(arr[i],1);
    }
    else{
    hp.put(arr[i], hp.get(arr[i])+1);
    }
    }
    int tot =0;
    System.out.println("hashmap is"+hp);
    //boolean n = false;
    Iterator it = hp.entrySet().iterator();
    boolean n = true;
    while (it.hasNext()) {
    Map.Entry pair = (Map.Entry)it.next();
    char hkey = (char) pair.getKey();
    System.out.println("hkey is" + hkey);
    int hval = (int)pair.getValue();
    System.out.println("hvalue is" + hval);
    if(hval % 2 == 0){

            tot += hval;
        }
        else{
             n = false;
            tot += hval-1;
        } 
    }
        if(n == false){
         return tot +1;
        }
        else{
            return tot;
        }
    }
    

    }


Log in to reply
 

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