Tushar
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
public static int findCount(char[][] matrix, int x, int y, char c){
int count = 0;
for(int i = 0; i < x; i++){
for(int j = 0; j < y; j++){
//Horizontal
if(j < y-2 && matrix[i][j]==c && matrix[i][j+1]==c && matrix[i][j+2]==c){
count++;
}
//Vertical
if(i < x-2 && matrix[i][j]==c && matrix[i+1][j]==c && matrix[i+2][j]==c){
count++;
}
//Diagonal left to right
if(i < x-2 && j < y-2 && matrix[i][j]==c && matrix[i+1][j+1]==c && matrix[i+2][j+2]==c){
count++;
}
}
for(int j = y-1; j > 0; j--){
//Diagonal right to left
if(i < x-2 && j > 1 && matrix[i][j]==c && matrix[i+1][j-1]==c && matrix[i+2][j-2]==c){
count++;
}
}
}
return count;
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Nice Solution but it will give StringIndexOutOfBoundException. To Avoid that, add this code snippet after For Loop:-
- Tushar April 15, 2014//For the end case
sb.append(times+""+repeat);