Heba
BAN USERpublic class LongestPallindrome {
public static void main(String[] args) {
String str = "DEFABCBAYT";
int l =str.length();
if(str.trim().length()==0)
{
System.out.println("String is empty");
System.exit(0);
}
String maxPal = SubString(str,l);
if(maxPal != null)
System.out.println(maxPal);
else
System.out.println("No Pallindrome in String");
}
public static String SubString(String str, int l) {
String str1 = "",str2 = "";
int max =0,l1 = 0;
for(int i =-1;i<l-1;i++)
{
str1 ="";
for(int j =i+1;j<l;j++){
str1 = str1+str.charAt(j);
l1 = str1.length();
if(pallindrome(str1,l1) )
{
if(max<l1)
{
max=l1;
str2 = str1;
}
}
}
}
if(max >0)
return str2;
else
return null;
}
public static boolean pallindrome(String str1,int l) {
String str2 = "";
for(int i= l-1;i>=0;i--)
{
str2= str2+str1.charAt(i);
}
if(str2.equals(str1))
return true;
else
return false;
}
}
/* This is for 6x6 Matrix, Similarly you can do for 2x2 Matrix*/
public class SpiralPattern {
public static void main(String[] args) {
int arr[][]={{1, 2, 3, 4, 5, 6},
{7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24},
{25, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36}};
int startRow = 0;
int col = arr[0].length;
SpiralPrint(arr,startRow,col);
}
private static void SpiralPrint(int[][] arr, int startRow, int col) {
int i,j;
for(i=startRow;i<arr.length;i++)
{
for(j=0;j<col;j++)
{
if(j==col-1||i==startRow)
System.out.print(arr[i][j]+"\t");
else
System.out.print("\t");
}
System.out.print("\n");
}
if(startRow<arr.length)
SpiralPrint(arr,startRow+1,col-1);
}
}
public class SymbolMatch {
public static void main(String[] args) {
String str[] ={"Amazon","Microsoft","Google"," "};
String sym[]={"i", "Am", "zon" ,"cro", "Na", "le", "soft"};
int l1 = str.length;
int l2 = sym.length;
String temp="";
int i,j,len=0;
for(i=0;i<l1;i++)
{
for(j=0;j<l2;j++)
{
if((str[i].contains(sym[j]))&&(len<sym[j].length()))
{
len = sym[j].length();
temp = sym[j];
}
}
if(len>0)
{
int k = str[i].indexOf(temp);
str[i] = str[i].substring(0,k)+"["+str[i].substring(k, k+len)+"]"+str[i].substring(k+len);
temp="";
len=0;
}
}
for(i=0;i<l1;i++)
System.out.println(str[i]);
}
}
public class CountDuplicate {
public static void main(String[] args) {
String str = "abcdbababac";
int l =str.length();
int i=0;
String output="";
int count[] = new int[26];
if(l==0)
{
System.out.println("The string is empty");
System.exit(0);
}
for(i=0;i<l;i++)
{
count[str.charAt(i)-'a']++;
}
for(i=0;i<26;i++)
{
if(count[i]!=0)
output = output + (char)(i+'a') + count[i];
}
System.out.println(output);
}
}
- Heba May 27, 2016