zsljulius
BAN USER@Urik Very smart solution. Here is my implementation in python for your solution.
def IsIsoMorphic(a,b):
dictA = dictB = defaultdict(str)
for i,c in enumerate(a):
dictA[c] = i
dictB[b[i]] = i
return encode(b,dictB) == encode(a,dictA)
def encode(a,dic):
return ''.join(str(dic[c]) for c in a)
Here is an implementation of Sieve of Eratosthenes in Python
def primesLessThan(n):
l = [True for i in xrange(3,n+1)]
for i in range(2, int(sqrt(n))+1):
for j,elem in enumerate(l):
if (j+1) % i == 0 and elem:
l[j] = False
return [i+1 for i,x in enumerate(l) if x]
public class SpiralPrinting {
public static void main(String args[]){
char[][] matrix =
{
{'i', 'l', 'o', 'v', 'e' },
{'d', 'i', 'n', 't', 'e' },
{'n', 'e', 'w', 'e', 'p' },
{'a', 'i', 'v', 'r', 'i' },
{'m', 'a', 'x', 'e', 'c' }
};
printMatrixSpiral(matrix);
}
public static void printMatrixSpiral(char[][] matrix){
int numRows = matrix.length;
int numColumns = matrix[0].length;
int totalNodes = numRows*numColumns;
int nodesVisted = 0;
int startRow =0;
int startColumn = 0;
while (nodesVisted < totalNodes){
int column =startColumn;
while (column<numColumns-1){
System.out.print(matrix[startRow][column]);
column++;
nodesVisted++;
}
int row =startRow;
while (row<numRows-1){
System.out.print(matrix[row][column]);
row++;
nodesVisted++;
}
while (column>startColumn){
System.out.print(matrix[row][column]);
column--;
nodesVisted++;
}
while (row>=startRow){
System.out.print(matrix[row][column]);
row--;
nodesVisted++;
}
startRow++;
startColumn++;
numColumns--;
numRows--;
}
}
}
Yet another Python Implementation with O(n) space and time.
- zsljulius November 08, 2013