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;
}
相关推荐
aaaameliaaa20 分钟前
字符函数和字符串函数
c语言·笔记·算法
夜月yeyue26 分钟前
AUTOSAR CP 从上电到 Runnable
c语言·网络·tcp/ip·车载系统
城管不管1 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
月疯2 小时前
二分法算法(水平等分图形面积)
算法
豆瓣鸡2 小时前
算法日记 - Day3
java·开发语言·算法
白白白小纯2 小时前
算法篇—反转链表
c语言·数据结构·算法·leetcode
Achou.Wang2 小时前
深入理解go语言-第5章 并发编程——Go的灵魂
大数据·算法·golang
The Chosen One9853 小时前
高进度算法模板速记(待完善)
java·前端·算法
小羊先生car3 小时前
RTOS-F429-HAL-绝对延时和相对延时(2026/7/31)
c语言·rtos
圣保罗的大教堂5 小时前
leetcode 3517. 最小回文排列 I 中等
leetcode