Description

You are given an array of strings equations that represent relationships between variables where each string equations[i] 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.

Examples

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
equations[i].length == 4
equations[i][0] is a lowercase letter.
equations[i][1] is either ‘=’ or ‘!’.
equations[i][2] is ‘=’.
equations[i][3] is a lowercase letter.

思路

感觉就是用并查集,判断两个单词是否在同一个簇中,要注意的是,需要先进行 “==” 的union,再进行 “!=” 的判断,因为没有操作来存储 “!=” 的约束

代码

class Solution {public void union(int[] parents, int x, int y) {int px = findParent(parents, x);int py = findParent(parents, y);if (px != py)parents[px] = py;}public int findParent(int[] parents, int x) {if(parents[x] != x)parents[x] = findParent(parents, parents[x]);return parents[x];}public boolean equationsPossible(String[] equations) {int[] parents = new int[26];for (int i = 0; i < parents.length; i++)parents[i] = i;for (String eq: equations) {if (eq.charAt(1) == '=')union(parents, eq.charAt(0) - 'a', eq.charAt(3) - 'a');}for (String eq: equations) {if (eq.charAt(1) == '!') {int x = findParent(parents, eq.charAt(0) - 'a');int y = findParent(parents, eq.charAt(3) - 'a');if (x == y)return false;}}return true;}
}

#990 Satisfiability of Equality Equations相关推荐

  1. 990. Satisfiability of Equality Equations

    Title 给定一个由表示变量之间关系的字符串方程组成的数组,每个字符串方程 equations[i] 的长度为 4,并采用两种不同的形式之一:"a==b" 或 "a!= ...

  2. word文档封面免费下载_为Word 2007文档添加封面

    word文档封面免费下载 I've been using Microsoft Word since 1985, starting with Word for Mac and eventually mo ...

  3. 如何快速准备面试中的算法,获得 Offer?

    如何快速准备面试中的算法,获得 Offer? 现如今越来越多的公司在面试过程中会考察数据结构和算法.在最近几年,难度颇有上升趋势.因此作为求职者,在面试前刷刷题似乎已经成为准备过程中必不可少的环节了. ...

  4. Optimal Trajectory Generation for Autonomous Vehicles Under Centripetal Acceleration Constraint [翻译]

    Optimal Trajectory Generation for Autonomous Vehicles Under Centripetal Acceleration Constraints for ...

  5. 990. 等式方程的可满足性

    链接:990. 等式方程的可满足性 题解:https://www.yuque.com/liweiwei1419/algo/gq157y class Solution {public:vector< ...

  6. 【Applied Algebra】求解布尔方程(Boolean Equations)的4个高效baseline算法

    求解布尔方程(Boolean Equations)的4个高效baseline算法 求解布尔方程(Boolean Equations)是理论计算机中的基本问题之一;事实上,求解Fq\mathbb{F}_ ...

  7. AN OPEN-SOURCE SPEAKER GENDER DETECTION FRAMEWORK FOR MONITORING GENDER EQUALITY

    AN OPEN-SOURCE SPEAKER GENDER DETECTION FRAMEWORK FOR MONITORING GENDER EQUALITY 监测两性平等的开源说话人性别检测框架 ...

  8. Linear equations

    In mathematics, a linear equation is an equation that may be put in the form a 1 x 1 + - + a n x n + ...

  9. Linear-time zero-knowledge proofs for arithmetic circuit satisfiability 学习笔记

    1. 引言 Bootle等人2017年论文<Linear-time zero-knowledge proofs for arithmetic circuit satisfiability> ...

最新文章

  1. 4.总结近5周以来的github上的工作情况,以图表方式分析你小组的工作情况、存在的问题及解决的方案。(尤心心)...
  2. 如何从管理IT服务提供商获得最大收益
  3. JS function立即调用的几种写法
  4. 中小企业SaaS型软件BI的发展前景
  5. 详解CockroachDB事务处理系统
  6. vs.php中使用apache或IIS7进行外部调试
  7. 遵循PSR-4的自动加载
  8. 一文详解高精地图构建与SLAM感知优化建图策略
  9. html图片上传框架,HTML+JS图片上传源码
  10. Excel基础学习笔记
  11. Java父亲节贺卡,父亲节贺卡祝福语语句
  12. PostgreSql增删改(与mysql差异)
  13. 计算机里不显示u盘盘符,U盘不显示盘符怎么办 U盘没了盘符解决方法【详解】...
  14. python开发板卡驱动开发_树莓派开发板如何驱动LED灯
  15. Shell(Bash)输入一个IP 判断是否可用并进行ping 测试
  16. 智能终端信息安全概念(五):硬件安全技术—加密芯片
  17. Itext 将html 字符串生成PDF 方法
  18. VB中使用DirectX库的简明教程(3)
  19. 数万美金奖励+中美往返差旅+国际周门票!万向区块链黑客马拉松硅谷站开始报名!
  20. 【mcuclub】时钟模块DS1302

热门文章

  1. 使用Git Bash的scp复制文件到Linux
  2. dlib重新训练dlib_face_recognition_resnet_model_v1.dat
  3. 1.2 网络爬虫分类
  4. VMWare 文件格式详解 .VMX .VMSD .VMDK
  5. 使用 Mypy 检查 30 万行 Python 代码,总结出 3 大痛点与 6 个技巧!
  6. 悬崖寻路问题Sarsa和QLearning实现
  7. Ice飞冰初始化《二》
  8. app公司制作app的方法和步骤
  9. 启动kafka2.8报afka.common.KafkaException: No `meta.properties` found in /tmp/kraft-combined-logs
  10. 每日分享 环境报错:Exception in thread “main“ java.lang.RuntimeException: Cannot create staging directory