class Solution {
public List<Integer> addToArrayForm(int[] A, int K) {
int n = A.length;
int c = K;
for(int i = A.length-1;i >= 0;i--){
A[i] += c;
c = A[i] / 10;
A[i] %= 10;
}
List<Integer> ret = new ArrayList<>();
while(c > 0){
ret.add(0, c%10);
c /= 10;
}
for(int v : A)ret.add(v);
return ret;
}
}
class Solution {
public boolean equationsPossible(String[] equations) {
DJSet ds = new DJSet(26);
for(String e : equations){
if(e.charAt(1) == '='){
int l = e.charAt(0)-'a';
int r = e.charAt(3)-'a';
ds.union(l, r);
}
}
for(String e : equations){
if(e.charAt(1) == '!'){
int l = e.charAt(0)-'a';
int r = e.charAt(3)-'a';
if(ds.equiv(l, r)){
return false;
}
}
}
return true;
}
public class DJSet {
public int[] upper;
public DJSet(int n) {
upper = new int[n];
Arrays.fill(upper, -1);
}
public int root(int x) {
return upper[x] < 0 ? x : (upper[x] = root(upper[x]));
}
public boolean equiv(int x, int y) {
return root(x) == root(y);
}
public boolean union(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (upper[y] < upper[x]) {
int d = x;
x = y;
y = d;
}
upper[x] += upper[y];
upper[y] = x;
}
return x == y;
}
public int count() {
int ct = 0;
for (int u : upper)
if (u < 0)
ct++;
return ct;
}
}
}