Amazon Interview Question
SDE-2sCountry: India
public class Palindrome2D {
static char[][] matrix = new char[][]{
{'a', 'b', 'c', 'd'},
{'c', 'b', 'a', 'c'},
{'c', 'a', 'a', 'c'},
{'a', 'b', 'c', 'd'}};
static int MAX = (matrix.length * matrix.length) / 2;
static int[][] move = new int[][]{
{0, 1},
{1, 0},
{0, -1},
{-1, 0}};
static Stack<Position> positions = new Stack<Position>();
static Map<Character, Integer> count = new HashMap<Character, Integer>();
static Set<String> palindromes = new HashSet<String>();
public static void main(String arg[]) {
int[][] visited = new int[matrix.length][matrix.length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
if (count.get(matrix[i][j]) == null) {
count.put(matrix[i][j], 0);
}
count.put(matrix[i][j], count.get(matrix[i][j]) + 1);
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
if (count.get(matrix[i][j]) != 1) {
find(0, visited, i, j, "" + matrix[i][j]);
}
}
}
}
private static void find(int phase, int[][] visited, int i, int j, String str) {
count.put(matrix[i][j], count.get(matrix[i][j]) - 1);
visited[i][j] = 1;
if (phase == 0) {
positions.add(new Position(i, j));
}
if (phase == 2) {
if (positions.empty()) {
if (!palindromes.contains(str)) {
palindromes.add(str);
System.out.println(str);
}
visited[i][j] = 0;
count.put(matrix[i][j], count.get(matrix[i][j]) + 1);
return;
}
}
for (int[] aMove : move) {
int newI = i + aMove[0];
int newJ = j + aMove[1];
if (newI < 0 || newJ < 0) {
continue;
}
if (newI >= matrix.length || newJ >= matrix.length) {
continue;
}
if (visited[newI][newJ] == 1) {
continue;
}
if (phase == 0 && count.get(matrix[newI][newJ]) == 0) {
continue;
}
if (positions.size() > MAX) {
continue;
}
if (phase == 0) {
positions.add(new Position(newI, newJ));
find(2, visited, newI, newJ, str + matrix[newI][newJ]);
positions.pop();
find(2, visited, newI, newJ, str + matrix[newI][newJ]);
find(0, visited, newI, newJ, str + matrix[newI][newJ]);
}
if (phase == 2) {
Position p = positions.peek();
if (matrix[newI][newJ] == matrix[p.x][p.y]) {
positions.pop();
find(2, visited, newI, newJ, str + matrix[newI][newJ]);
positions.add(p);
}
}
}
if (phase == 0) {
visited[i][j] = 0;
count.put(matrix[i][j], count.get(matrix[i][j]) + 1);
positions.pop();
}
}
static class Position {
int x;
int y;
Position(int x, int y) {
this.x = x;
this.y = y;
}
}
}
I think this should work, The idea is that we iterative check for all the palindromes in all four directions, forward, downward, back diagonal, front diagonal. Checking in eight directions is not required since you will get the same palindrome again which you don't want. Also to sort by length and remove duplicate, i use a map and an multimap, multimap keeps array sortedby key which is length in this case. In the end we dump the map on to the STDOUT. There are still some missing corner cases, since i wrote this in an online test and didnot have enough time to cover all corners.
#include <iostream>
#include <map>
using namespace std;
std::map <string, int > palindromes;
std::multimap <int, string> palindromes_sorted;
void Palindromefinder(char** matrix, int N, int M);
void findpalindromes(char** matrix, int startx, int starty, int N,int M, int len);
bool ispalindrome(char* str, int len);
void print_multimap();
int move[][2]=
{{0,1},// left
{1,0}, // down
{1,1}, // forward diagonal
{-1,1} // backward diagonal
};
void print_multimap()
{
for( multimap<int, string>::iterator it = palindromes_sorted.begin(),
end = palindromes_sorted.end(); it != end;
it = palindromes_sorted.upper_bound(it->first))
{
cout << it->second << endl;
}
}
int main()
{
int N, M;
cin >> N;
cin >> M;
char** matrix= new char*[N];
int i= N;
while (i--> 0)
{
matrix[N-i-1]= new char[M];
cin >> matrix[N-i-1];
}
Palindromefinder(matrix, N, M);
return 0;
}
void Palindromefinder(char** matrix, int N, int M)
{
int i=0, j=0;
for(; i<N ; i++)
{
for(; j<M; j++)
{
// start for all the positions
findpalindromes(matrix,i,j,N,M,1/* min length=2*/);
}
}
print_multimap();
}
bool ispalindrome(char* str, int len)
{
if(len== 0 || len == 1)
return true;
if (*str == *(str+len-1))
return(ispalindrome(str+1, len-2));
return false;
}
void findpalindromes(char** matrix, int startx, int starty, int N, int M, int len)
{
int i=0, movex, movey, indexx, indexy;
int checked=0;
for(;i< 4; i++)
{
indexx= len*move[i][0];
indexy= len*move[i][1];
movex= move[i][0];
movey= move[i][1];
// check bounds
if (((startx+indexx) < N && (startx+indexx) > -1) &&
((starty+indexy )< M) && ((starty+indexy) > -1))
{
checked++;
// copy it in a temp string
char* temp= new char[len];
int k=0,p=startx, q=starty;
for (;k<=len; p+=movex, q+=movey, k++)
{
temp[k]=matrix[p][q];
}
if(ispalindrome(temp, len+1))
{
string* temp_str= new string(temp);
if (palindromes.count(*temp_str) == 0)
{
palindromes.insert(pair<string, int>(*temp_str, len+1));
palindromes_sorted.insert(pair<int, string>(len+1, *temp_str));
}
delete temp_str;
}
delete temp;
}
else
continue;
}
if(checked > 0)
findpalindromes(matrix, startx, starty, N, M, len+1);
}
Is there a minimum length specified?
- bytecode January 28, 2014If no, then even a single letter is a palindrome.
Also we cannot traverse a position again right? For example : a[0][0] --> a[1][0] then again a[0][0]? is this possible?