Problem solution · C++

Check if Word Can Be Placed In Crossword

Check if Word Can Be Placed In Crossword: a C++ solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Direct simulation
Source
walkccc LeetCode Solutions
Length
55 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For Check if Word Can Be Placed In Crossword, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 55 lines of C++ from the credited upstream file 2018.cpp.
  • The implementation visibly relies on sequence storage.
  • 9 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeCheck if Word Can Be Placed In Crossword · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  bool placeWordInCrossword(vector<vector<char>>& board, string word) {    for (const vector<vector<char>>& state : {board, getRotated(board)})      for (const vector<char>& chars : state)        for (const string& token : getTokens(join(chars)))          for (const string& letters :               {word, string{word.rbegin(), word.rend()}})            if (letters.length() == token.length())              if (canFit(letters, token))                return true;    return false;  }  private:  vector<vector<char>> getRotated(const vector<vector<char>>& board) {    const int m = board.size();    const int n = board[0].size();    vector<vector<char>> rotated(n, vector<char>(m));    for (int i = 0; i < m; ++i)      for (int j = 0; j < n; ++j)        rotated[j][i] = board[i][j];    return rotated;  }   vector<string> getTokens(const string& row) {    vector<string> tokens;    int start = 0;    int end;    string token;    do {      end = row.find('#', start);      token = row.substr(start, end - start);      if (!token.empty())        tokens.push_back(token);      start = end + 1;    } while (end != string::npos);    return tokens;  }   string join(const vector<char>& chars) {    string joined;    for (const char c : chars)      joined += c;    return joined;  }   bool canFit(const string& letters, const string& token) {    for (int i = 0; i < letters.length(); ++i)      if (token[i] != ' ' && token[i] != letters[i])        return false;    return true;  }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗