Problem solution · C++

Check for Contradictions in Equations

Check for Contradictions in Equations: a C++ solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Depth-first search
Source
walkccc LeetCode Solutions
Length
48 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

Depth-first search

For Check for Contradictions in Equations, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 48 lines of C++ from the credited upstream file 2307.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • 4 loop blocks detected, together with recursive traversal.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

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 for Contradictions in Equations · C++C++
Use this to learn the idea, then write your own version.
class Solution { public:  bool checkContradictions(vector<vector<string>>& equations,                           vector<double>& values) {    // Convert `string` to `int` for a better perfermance.    unordered_map<string, int> strToInt;     for (const vector<string>& equation : equations) {      const string& u = equation[0];      const string& v = equation[1];      if (!strToInt.contains(u))        strToInt[u] = strToInt.size();      if (!strToInt.contains(v))        strToInt[v] = strToInt.size();    }     vector<vector<pair<int, double>>> graph(strToInt.size());    vector<double> seen(graph.size());     for (int i = 0; i < equations.size(); ++i) {      const int u = strToInt.at(equations[i][0]);      const int v = strToInt.at(equations[i][1]);      graph[u].emplace_back(v, values[i]);      graph[v].emplace_back(u, 1 / values[i]);    }     for (int i = 0; i < graph.size(); ++i)      if (!seen[i] && dfs(graph, i, seen, 1.0))        return true;     return false;  }  private:  bool dfs(const vector<vector<pair<int, double>>>& graph, int u,           vector<double>& seen, double val) {    if (seen[u])      return abs(val / seen[u] - 1) > 1e-5;     seen[u] = val;    for (const auto& [v, w] : graph[u])      if (dfs(graph, v, seen, val / w))        return true;     return false;  }}; 

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 ↗