C++ solution O(N*M)


  • 0
    G
    class Solution {
    public:
        
        int NumOfColumns(const string &str, vector<vector<char> > &picture,int N)
        {
            int Total = 0;
            
            for(int i = 0; i < picture[0].size(); ++i)
            {
                if(str[i] == 'B')
                {
                    int Count = 0;
                    
                    for(int j = 0; j < picture.size(); ++j)
                    {
                        Count += (picture[j][i] == 'B');
                    }
                    
                    if(Count == N)
                    {
                        Total++;
                    }
                }
            }
            
            return (Total);
        }
    
        int findBlackPixel(vector<vector<char>>& picture, int N) 
        {
            unordered_map<string,vector<pair<int,int> > > M;
            
            for(int i = 0; i < picture.size(); ++i)
            {
                string Curr;
                int Blacks = 0;
                
                for(int j = 0; j < picture[i].size(); ++j)
                {
                    Curr.push_back(picture[i][j]);
                    Blacks += (picture[i][j] == 'B');
                    
                }
                
                M[Curr].push_back({i,Blacks});
            }
            
            int Total = 0;
            
            for(auto &m : M)
            {
                if(m.second.size() == N && m.second[0].second == N)
                {
                    int T = NumOfColumns(m.first,picture,N);
                    Total += T*N;
                }
            }
            
            return (Total);
        }
    };

Log in to reply
 

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