CinderillaLi
BAN USERpublic class printMatrixDiagonally {
public static void main(String[] args){
int row = 2 , col =3;
int[][] input = new int[row][col];
for(int i =0 ; i< row ; i++){
for(int j= 0 ; j<col ;j ++){
input[i][j] = i+j;
}
}
printStack(input);
//Here is to print it out diagonally.
printStackDiag(input);
}
//this method is to print out the matrix.
public static void printStack(int[][] input){
int row = input.length, col = input[0].length;
for(int i =0 ;i <row ; i++){
for(int j=0 ; j<col ;j++){
System.out.print(" "+input[i][j]);
}
System.out.println();
}
}
public static void printStackDiag(int[][] input){
//. We need to print one line where their sum of i, j index is the same.
//total is the max of i, j sum.
int row = input.length , col = input[0].length, total = row+col -2 , sum = 0;
while(sum>=0 && sum <= total){
for(int i = 0 ; i< row ; i++){
int j= sum -i;
if(j>=0 && j< col){
System.out.print(" "+input[i][j]);
}
}
sum++;
System.out.println();
}
}
}
Thank you, this is simple and efficient.
- CinderillaLi February 25, 2014
Iterate the string b, if find one char which is contained by a, compare the following a.length() substring of b with a, to see if this substring is anagram of a.
- CinderillaLi March 31, 2014public static boolean isAnagram(String a, String b){
char[] aChars = a.toCharArray();
char[] bChars = b.toCharArray();
Arrays.sort(aChars);
Arrays.sort(bChars);
return String.copyValueOf(aChars).equalsIgnoreCase(String.copyValueOf(bChars));
}
public static void main(String[] args){
String a ="xyd";
String b= "afdgzyxksldfm";
boolean flag = false;
for(int i = 0; i<b.length(); i++){
String s = b.substring(i, i+1);
if(a.contains(s)){
s= b.substring(i, i+a.length());
if(isAnagram(a, s)){
flag = true;
break;
}
}
}
System.out.println("If string a "+a+" is contained by b "+b+" "+flag);
}