Decide Whether Letter-Variable Equalities and Inequalities Are Consistent

Quick Overview

Decide Whether Letter-Variable Equalities and Inequalities Are Consistent. Each four-character equation has the form x==y or x!=y, with lowercase single-letter variables that may be the same letter.

Decide Whether Letter-Variable Equalities and Inequalities Are Consistent

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Each four-character equation has the form `x==y` or `x!=y`, with lowercase single-letter variables that may be the same letter. Return whether integer values can be assigned to the variables so every equation is satisfied. ```cpp class Solution { public: bool equationsPossible(vector<string>& equations); }; ``` ### Examples **Example 1:** ```text Input: equations = ["a==b","b==c","a!=c"] Output: false Explanation: The equalities force a,b,and c to share one value, contradicting a!=c. ``` **Example 2:** ```text Input: equations = ["a!=b","b!=c","a==c"] Output: true Explanation: Assign a=c=0 and b=1. ``` ### Constraints - `1 <= equations.length <= 500`. - Each string has length4: lowercase variable, `=` or `!`, `=`, lowercase variable. ### Contract considerations The function returns satisfiability, not an arbitrary assignment. Repeated relations are permitted by the supplied format. ```hint Read the boundary condition Equalities form equivalence classes; an inequality is impossible only when both ends fall in one class. ``` ### Boundary reminders The function returns satisfiability, not an arbitrary assignment. Repeated relations are permitted by the supplied format.

Overview: Decide Whether Letter-Variable Equalities and Inequalities Are Consistent. Each four-character equation has the form x==y or x!=y, with lowercase single-letter variables that may be the same letter.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each four-character equation has the form x==y or x!=y, with lowercase single-letter variables that may be the same letter. Return whether integer values can be assigned to the variables so every equation is satisfied.

class Solution {
public:
    bool equationsPossible(vector<string>& equations);
};

Examples

Example 1:

Input: equations = ["a==b","b==c","a!=c"]
Output: false
Explanation: The equalities force a,b,and c to share one value, contradicting a!=c.

Example 2:

Input: equations = ["a!=b","b!=c","a==c"]
Output: true
Explanation: Assign a=c=0 and b=1.

Constraints

  • 1 <= equations.length <= 500 .
  • Each string has length4: lowercase variable, = or ! , = , lowercase variable.

Contract considerations

The function returns satisfiability, not an arbitrary assignment. Repeated relations are permitted by the supplied format.

Boundary reminders

The function returns satisfiability, not an arbitrary assignment. Repeated relations are permitted by the supplied format.