**Ambigram Detection- Google Top Interview Questions**

### Problem Statement :

You are given a lowercase alphabet string s and a two-dimensional list of strings pairs. Each element in pairs contains [a, b] meaning that character a and b are considered equivalent. We say that if a and b are equivalent and b and c are equivalent, then a and c are also equivalent. Also, any character a is equivalent to itself. Return whether s is a palindrome given the equivalence relations. Constraints 0 ≤ n ≤ 100,000 where n is the length of s 0 ≤ p ≤ 100,000 where p is the length of p Example 1 Input s = "acba" pairs = [ ["b", "c"] ] Output True Explanation Since "b" = "c", we have "acba" = "acca", which is a palindrome. Example 2 Input s = "zz" pairs = [] Output True Explanation "z" is equivalent to itself. Example 3 Input s = "ac" pairs = [ ["a", "b"], ["b", "c"] ] Output True Explanation Since "a" = "c", we have "ac" = "cc", which is a palindrome.

### Solution :

` ````
Solution in C++ :
bool solve(string s, vector<vector<string>>& pairs) {
vector<int> parent(26);
iota(parent.begin(), parent.end(), 0);
function<int(int)> find = [&](int x) {
return parent[x] == x ? x : parent[x] = find(parent[x]);
};
auto join = [&](int a, int b) { parent[find(a)] = parent[find(b)]; };
for (auto& p : pairs) {
int a = p[0][0] - 'a';
int b = p[1][0] - 'a';
join(a, b);
}
for (char& c : s) {
int i = c - 'a';
c = find(i) + 'a';
}
int i = 0, j = (int)s.size() - 1;
while (i < j) {
if (s[i] ^ s[j]) return false;
i++, j--;
}
return true;
}
```

` ````
Solution in Java :
import java.util.*;
class Solution {
public boolean solve(String s, String[][] pairs) {
DisjointSetUnion dsu = new DisjointSetUnion(26);
for (String[] p : pairs) {
int a = p[0].charAt(0) - 'a';
int b = p[1].charAt(0) - 'a';
dsu.connect(a, b);
}
for (int i = 0; i < s.length(); i++) {
int j = s.length() - 1 - i;
int a = s.charAt(i) - 'a';
int b = s.charAt(j) - 'a';
if (!dsu.connected(a, b))
return false;
}
return true;
}
static class DisjointSetUnion {
public int[] parent;
public int[] weight;
public int count;
public DisjointSetUnion(int N) {
count = N;
parent = new int[N];
for (int i = 0; i < N; i++) parent[i] = i;
weight = new int[N];
Arrays.fill(weight, 1);
}
//"find"
public int root(int p) {
if (p == parent[p])
return p;
return parent[p] = root(parent[p]);
}
//"union"
public void connect(int p, int q) {
p = root(p);
q = root(q);
if (p == q)
return;
if (weight[p] < weight[q]) {
parent[p] = q;
weight[q] += weight[p];
} else {
parent[q] = p;
weight[p] += weight[q];
}
count--;
}
public boolean connected(int p, int q) {
return root(p) == root(q);
}
}
}
```

` ````
Solution in Python :
class Solution:
def solve(self, s, pairs):
parent = {}
for c in s:
parent[c] = c
def find(c):
if c not in parent or parent[c] == c:
return c
parent[c] = find(parent[c])
return parent[c]
def union(a, b):
a = find(a)
b = find(b)
if a != b:
parent[b] = a
for s1, e1 in pairs:
union(s1, e1)
s = [find(x) for x in s]
return s == s[::-1]
```

## View More Similar Problems

## Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

View Solution →## Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and

View Solution →## Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →## No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →## Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →## Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →