Esempio n. 1
0
    bool exist(vector<vector<char>>& board, string word)
    {
        if(board.empty() || board[0].empty())
        {
            return false;
        }

        int row = board.size();
        int col = board[0].size();

        typedef std::unordered_map<char, std::vector<std::pair<int,int>>> CharMap;

        CharMap charMap;
        for(int i = 0; i < board.size(); ++i)
        {
            for(int j = 0; j < board[i].size(); ++j)
            {
                char c = board[i][j];
                if(charMap.find(c) == charMap.end())
                {
                    charMap[c] = std::vector<std::pair<int,int>>{std::make_pair(i, j)};
                }
                else
                {
                    charMap[c].push_back(std::make_pair(i, j));
                }
            }
        }

        vector<vector<bool>> visited(row, vector<bool>(col, false));

        if(charMap.find(word[0]) != charMap.end())
        {
            for(const auto& p : charMap[word[0]])
            {
                visited[p.first][p.second] = true;
                if(impl(board, row, col, visited, word, 1, p.first, p.second))
                {
                    return true;
                }
                visited[p.first][p.second] = false;
            }
        }

        return false;
    }
Esempio n. 2
0
void removeDuplicates(Node* head) {
  CharMap map;
  Node* n = head;
  do {
    map[n->val]++;
    n = n->next;
  } while(n);


  for(CharMap::const_iterator it = map.begin(); it != map.end(); ++it) {
    int cnt = it->second;
    while(cnt > 1) {
      head = removeNode(head, it->first);
      --cnt;
    }
  }
}