Skip to content

Latest commit

 

History

History
278 lines (175 loc) · 5.46 KB

File metadata and controls

278 lines (175 loc) · 5.46 KB

中文文档

Description

Given an array equations of strings that represent relationships between variables, each string equations[i] has length 4 and takes one of two different forms: "a==b" or "a!=b".  Here, a and b are lowercase letters (not necessarily different) that represent one-letter variable names.

Return true if and only if it is possible to assign integers to variable names so as to satisfy all the given equations.

 

Example 1:

Input: ["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: ["b==a","a==b"]

Output: true

Explanation: We could assign a = 1 and b = 1 to satisfy both equations.

Example 3:

Input: ["a==b","b==c","a==c"]

Output: true

Example 4:

Input: ["a==b","b!=c","c==a"]

Output: false

Example 5:

Input: ["c==c","b==d","x!=z"]

Output: true

 

Note:

  1. 1 <= equations.length <= 500
  2. equations[i].length == 4
  3. equations[i][0] and equations[i][3] are lowercase letters
  4. equations[i][1] is either '=' or '!'
  5. equations[i][2] is '='

Solutions

Python3

class Solution:
    def equationsPossible(self, equations: List[str]) -> bool:
        p = [i for i in range(26)]

        def find(x):
            if p[x] != x:
                p[x] = find(p[x])
            return p[x]

        for e in equations:
            a, r, b = ord(e[0]) - ord('a'), e[1:3], ord(e[3]) - ord('a')
            if r == '==':
                p[find(a)] = find(b)
        for e in equations:
            a, r, b = ord(e[0]) - ord('a'), e[1:3], ord(e[3]) - ord('a')
            if r == '!=' and find(a) == find(b):
                return False
        return True

Java

class Solution {
    private int[] p;

    public boolean equationsPossible(String[] equations) {
        p = new int[26];
        for (int i = 0; i < 26; ++i) {
            p[i] = i;
        }
        for (String e : equations) {
            int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
            String r = e.substring(1, 3);
            if ("==".equals(r)) {
                p[find(a)] = find(b);
            }
        }
        for (String e : equations) {
            int a = e.charAt(0) - 'a', b = e.charAt(3) - 'a';
            String r = e.substring(1, 3);
            if ("!=".equals(r) && find(a) == find(b)) {
                return false;
            }
        }
        return true;
    }

    private int find(int x) {
        if (p[x] != x) {
            p[x] = find(p[x]);
        }
        return p[x];
    }
}

C++

class Solution {
public:
    vector<int> p;

    bool equationsPossible(vector<string>& equations) {
        p.resize(26);
        for (int i = 0; i < 26; ++i)
            p[i] = i;
        for (auto e : equations)
        {
            int a = e[0] - 'a', b = e[3] - 'a';
            char r = e[1];
            if (r == '=')
                p[find(a)] = find(b);
        }
        for (auto e : equations)
        {
            int a = e[0] - 'a', b = e[3] - 'a';
            char r = e[1];
            if (r == '!' && find(a) == find(b))
                return false;
        }
        return true;
    }

    int find(int x) {
        if (p[x] != x)
            p[x] = find(p[x]);
        return p[x];
    }
};

Go

var p []int

func equationsPossible(equations []string) bool {
	p = make([]int, 26)
	for i := 1; i < 26; i++ {
		p[i] = i
	}
	for _, e := range equations {
		a, b := int(e[0]-'a'), int(e[3]-'a')
		r := e[1]
		if r == '=' {
			p[find(a)] = find(b)
		}
	}
	for _, e := range equations {
		a, b := int(e[0]-'a'), int(e[3]-'a')
		r := e[1]
		if r == '!' && find(a) == find(b) {
			return false
		}
	}
	return true
}

func find(x int) int {
	if p[x] != x {
		p[x] = find(p[x])
	}
	return p[x]
}

...