LeetCode //C - 990. Satisfiability of Equality Equations

990. Satisfiability of Equality Equations

You are given an array of strings equations that represent relationships between variables where each string equationsi is of length 4 and takes one of two different forms: "xi==yi" or "xi!=yi".Here, xi and yi are lowercase letters (not necessarily different) that represent one-letter variable names.

Return true if it is possible to assign integers to variable names so as to satisfy all the given equations, or false otherwise.

Example 1:

Input: equations = "a==b","b!=a"
Output: false
Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second.

There is no way to assign the variables to satisfy both equations.

Example 2:

Input: equations = "ba","ab"
Output: true
Explanation: We could assign a = 1 and b = 1 to satisfy both equations.

Constraints:
  • 1 <= equations.length <= 500
  • equationsi.length == 4
  • equationsi0 is a lowercase letter.
  • equationsi1 is either '=' or '!'.
  • equationsi2 is '='.
  • equationsi3 is a lowercase letter.

From: LeetCode

Link: 990. Satisfiability of Equality Equations


Solution:

Ideas:
  • Build groups using all "x==y" first (Union-Find / DSU).

  • Then for each "x!=y", if x and y end up in the same group → contradiction → false.

  • Otherwise, it's satisfiable → true.

Code:
c 复制代码
static int find(int parent[], int x) {
    if (parent[x] != x) parent[x] = find(parent, parent[x]);
    return parent[x];
}

static void unite(int parent[], int rank[], int a, int b) {
    int ra = find(parent, a);
    int rb = find(parent, b);
    if (ra == rb) return;

    if (rank[ra] < rank[rb]) {
        parent[ra] = rb;
    } else if (rank[ra] > rank[rb]) {
        parent[rb] = ra;
    } else {
        parent[rb] = ra;
        rank[ra]++;
    }
}

bool equationsPossible(char** equations, int equationsSize) {
    int parent[26], rank[26] = {0};

    for (int i = 0; i < 26; i++) parent[i] = i;

    // 1) Union all equalities.
    for (int i = 0; i < equationsSize; i++) {
        char *e = equations[i];
        if (e[1] == '=' && e[2] == '=') {
            int x = e[0] - 'a';
            int y = e[3] - 'a';
            unite(parent, rank, x, y);
        }
    }

    // 2) Check all inequalities against the union-find sets.
    for (int i = 0; i < equationsSize; i++) {
        char *e = equations[i];
        if (e[1] == '!' && e[2] == '=') {
            int x = e[0] - 'a';
            int y = e[3] - 'a';
            if (find(parent, x) == find(parent, y)) return false;
        }
    }

    return true;
}
相关推荐
小羊在睡觉2 小时前
力扣84. 柱状图中最大的矩形
后端·算法·leetcode·golang·go
3DVisionary2 小时前
蓝光三维扫描:医疗制造的精度焦虑怎么解
人工智能·算法·制造·蓝光三维扫描·医疗制造·三维检测·义齿检测
好评笔记2 小时前
机器学习面试八股——常用损失函数
人工智能·深度学习·算法·机器学习·校招
weixin_468466852 小时前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
sheeta19983 小时前
LeetCode 每日一题笔记 日期:2026.05.29 题目:3300. 最小元素
笔记·leetcode
_日拱一卒3 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
珂朵莉MM3 小时前
第七届全球校园人工智能算法精英大赛-算法巅峰赛产业命题赛第3赛季优化题--束搜索
人工智能·算法
Omics Pro4 小时前
首个!外源天然产物综合性代谢图谱
数据库·人工智能·算法·机器学习·r语言
voidmort4 小时前
3. 微调(Fine-tuning)与强化学习(RL)的核心思想
python·深度学习·算法
Bluetooth7304 小时前
c语言一维数组
c语言