Java Solution using HashSet and HashMap


  • 0
    D
    
    public class Solution {
        public int findLonelyPixel(char[][] picture) {
            HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
            int [] bb = new int[picture[0].length];
    
            for (int i = 0 ; i < picture.length ; i++){
                int k = -1;
                HashSet<Character> hs = new HashSet<Character>();
                boolean f1 = false;
                for (int j = 0 ; j < picture[0].length; j++){
                    if (picture[i][j] == 'B'){
                        bb[j] +=1;
                        f1 = hs.add('B');
                        if(f1){
                           k = j; 
                           if(hm.containsKey(k))        hm.remove(k);  
                        }
                        else k = -1;
                    }
                }
                
                if (k>-1){
                if(hm.containsKey(k)){
                    hm.remove(k);
                }
                else{
                    if (bb[k]==1)      hm.put(k,i); 
                }
              }   
            }
            return hm.size();
        }
    }
    
    

  • 1

    @deadman069
    Failed for this case:
    ["WBWW","BWWW","WBWB"]
    The answer should be 1.
    You output is 2.


  • 0
    D

    @yubad2000 Hey, Nice catch, updated the code with the fix.


Log in to reply
 

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