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;
}
相关推荐
Dola_Zou43 分钟前
工厂智能排产软件算法保护与按产线计费实战
算法·自动化·软件工程·软件加密
啦啦啦啦啦zzzz2 小时前
Logger的组装(c++20)
c++·算法·c++20
尾善爱看海3 小时前
前端算法与手写题集
前端·算法
繁星蓝雨4 小时前
C++的设计与演进大纲(如何从C语言一步步成长为C++)
c语言·开发语言·c++·c++历史·c++演进
程序员阿鹏4 小时前
为什么MySQL InnoDB选择B+树?
数据结构·数据库·b树·sql·mysql·算法·缓存
yutian06064 小时前
C: Unix UTC 时间戳转为 UTC+8 北京时间
c语言·unix
AI 思录4 小时前
Prompt 事故档案(八):日常表达被标为“待校准”,AI 的爹味语法从哪里来
大数据·人工智能·算法·prompt·用户体验·ai合规
月光船幽幽4 小时前
跨范式映射的稳定接口设计
人工智能·python·算法
2601_965742226 小时前
全媒体运营与短视频代运营,两者有什么区别?
大数据·数据结构·人工智能·算法·ai·媒体
wordbaby7 小时前
混合检索:两全其美的艺术
人工智能·算法