anandkiran2007
BAN USERpublic class PrintPattern {
public static String ReturnString(String s1)
{
int count = 1;
String s2 = "";
s1 = s1 + ".";
for(int i = 0 ; i <=s1.length(); i ++)
{
if(i == s1.length() -1)
{
break;
}
else if(s1.toLowerCase().charAt(i) == s1.toLowerCase().charAt(i+1) )
{
count++;
}
else
{
s2 = s2 + s1.toUpperCase().charAt(i) + String.valueOf(count);
count = 1;
}
}
return s2;
}
public static void main(String[] args)
{
String s1 = "WwwWWTTRRREEETOo";
System.out.println(ReturnString(s1));
}
}
here is my code ... hope it helps i have tested the given conditions
public class PatternMatch {
static boolean travasel[][];
public static boolean findPattern(char[][] arr,int[] cRows, int[] cColumns,int intRow, int intCol,char[] pattern)
{
travasel = new boolean[intRow][intCol];
for(int i = 0; i< intRow; i++)
{
for(int j = 0; j< intCol; j++)
{
travasel[i][j] = false;
}
}
for(int i = 0; i< intRow; i++)
{
for(int j = 0; j< intCol; j++)
{
if(arr[i][j] == pattern[0])
{
travasel[i][j] = true;
if(checkPattern(arr,cRows,cColumns,i,j,pattern,0))
return true;
}
}
}
return false;
}
public static boolean checkPattern(char[][] m,int[] cRows, int[] cColumns,int tRows,int tCol, char[] pattern,int index)
{
if (index == pattern.length - 1)
return true;
for(int i =0; i < cRows.length; i++)
{
if(tRows+cRows[i] >= 0 && tRows+cRows[i] <= m.length -1 && tCol+cColumns[i] >= 0 && tCol+cColumns[i] <= m[0].length -1 && travasel[tRows+cRows[i]][tCol+cColumns[i]] == false)
{
if(m[tRows+cRows[i]][tCol+cColumns[i]] == pattern[index +1])
{
travasel[tRows+cRows[i]][tCol+cColumns[i]] = true;
if(checkPattern(m,cRows,cColumns,tRows+cRows[i],tCol+cColumns[i],pattern,index + 1))
{
return true;
}
travasel[tRows+cRows[i]][tCol+cColumns[i]] = false;
}
}
}
return false;
}
public static void main(String[] args) {
int[] incRows = {-1,1,0,0,-1,-1,1,1};
int[] incColumns = {0,0,1,-1,-1,1,-1,1};
char[][] m = {{'a','b','c','d','e','f'},
{'z','n','a','b','c','f'}, // z n a b c f
{'f','g','f','a','b','c'}}; //f g f a b c
String pattern = "gng";
System.out.println(findPattern(m,incRows,incColumns,m.length,m[0].length,pattern.toCharArray()));
}
}
this will work only for array with 4 elements, if the inout array is dynamic it will fail
- anandkiran2007 March 22, 2014Here is my code
1. Identify the current index and set the value of that element as 1
2. Multiple the elements in the array , that will be product of 3 numbers
public class MultiplyNumber {
public static int[] Multiply(int[] arr,int len)
{
int[] output = new int[arr.length];
int start = 0;
int k = 1;
int temp = 0;
int currentIndex = 0;
for(int i = 0 ; i < arr.length; i ++)
{
start = 0;
k = 1;
temp = arr[i];
arr[i] = 1;
currentIndex = i;
while(start != len)
{
k = k * arr[start];
start ++;
}
arr[currentIndex] = temp;
output[i] = k;
}
return output;
}
public static void main(String[] args) {
int[] a = {2,3,1,4};
int[] out = Multiply(a,a.length);
for(int i = 0 ; i <out.length; i ++)
{
System.out.print(out[i]+",");
}
}
}
Using Array List
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class RemoveSameElements {
public static void RemoveSameNumber(List<Integer> myArr)
{
for(int i = 0 ; i < myArr.size(); i++)
{
if(myArr.get(i) == myArr.get(i+1))
{
myArr.remove(i);
myArr.remove(i);
if(myArr.size() > 1)
{
RemoveSameNumber(myArr);
}
}
}
System.out.println(myArr);
}
public static void main(String[] args) {
int[] arr = {1,2,2,1,4,5,6,6,5};
List<Integer> myArr = new ArrayList<>();
for(int i = 0 ; i < arr.length; i ++)
{
myArr.add(arr[i]);
}
RemoveSameNumber(myArr);
}
}
check this out
public class PatternMathc {
static boolean travasel[][];
public static boolean findPattern(char[][] arr,int[] cRows, int[] cColumns,int intRow, int intCol,char[] pattern)
{
travasel = new boolean[intRow][intCol];
for(int i = 0; i< intRow; i++)
{
for(int j = 0; j< intCol; j++)
{
travasel[i][j] = false;
}
}
for(int i = 0; i< intRow; i++)
{
for(int j = 0; j< intCol; j++)
{
if(arr[i][j] == pattern[0])
{
if(checkPattern(arr,cRows,cColumns,i,j,pattern,0))
return true;
}
}
}
return false;
}
public static boolean checkPattern(char[][] m,int[] cRows, int[] cColumns,int tRows,int tCol, char[] pattern,int index)
{
if (index == pattern.length - 1)
return true;
for(int i =0; i < cRows.length; i++)
{
if(tRows+cRows[i] >= 0 && tRows+cRows[i] <= m.length -1 && tCol+cColumns[i] >= 0 && tCol+cColumns[i] <= m.length -1)
{
if(m[tRows+cRows[i]][tCol+cColumns[i]] == pattern[index +1])
{
travasel[tRows+cRows[i]][tCol+cColumns[i]] = true;
if(checkPattern(m,cRows,cColumns,tRows+cRows[i],tCol+cColumns[i],pattern,index + 1))
{
return true;
}
travasel[tRows+cRows[i]][tCol+cColumns[i]] = false;
}
}
}
return false;
}
public static void main(String[] args) {
int[] incRows = {-1,1,0,0,-1,-1,1,1};
int[] incColumns = {0,0,1,-1,-1,1,-1,1};
char[][] m = {{'A','C','P','R','C'},
{'X','S','O','P','C'},
{'V','O','V','N','I'},
{'W','G','F','M','N'},
{'Q','A','T','I','T'}};
String pattern = "ACPOVFTAQWVXAC";
System.out.println(findPattern(m,incRows,incColumns,m.length,m.length,pattern.toCharArray()));
}
}
Please check out this
public class PatternMathc {
static boolean travasel[][];
public static boolean findPattern(char[][] arr,int[] cRows, int[] cColumns,int intRow, int intCol,char[] pattern)
{
travasel = new boolean[intRow][intCol];
for(int i = 0; i< intRow; i++)
{
for(int j = 0; j< intCol; j++)
{
travasel[i][j] = false;
}
}
for(int i = 0; i< intRow; i++)
{
for(int j = 0; j< intCol; j++)
{
if(arr[i][j] == pattern[0])
{
if(checkPattern(arr,cRows,cColumns,i,j,pattern,0))
return true;
}
}
}
return false;
}
public static boolean checkPattern(char[][] m,int[] cRows, int[] cColumns,int tRows,int tCol, char[] pattern,int index)
{
if (index == pattern.length - 1)
return true;
for(int i =0; i < cRows.length; i++)
{
if(tRows+cRows[i] >= 0 && tRows+cRows[i] <= m.length -1 && tCol+cColumns[i] >= 0 && tCol+cColumns[i] <= m.length -1)
{
if(m[tRows+cRows[i]][tCol+cColumns[i]] == pattern[index +1])
{
travasel[tRows+cRows[i]][tCol+cColumns[i]] = true;
if(checkPattern(m,cRows,cColumns,tRows+cRows[i],tCol+cColumns[i],pattern,index + 1))
{
return true;
}
travasel[tRows+cRows[i]][tCol+cColumns[i]] = false;
}
}
}
return false;
}
public static void main(String[] args) {
int[] incRows = {-1,1,0,0,-1,-1,1,1};
int[] incColumns = {0,0,1,-1,-1,1,-1,1};
char[][] m = {{'A','C','P','R','C'},
{'X','S','O','P','C'},
{'V','O','V','N','I'},
{'W','G','F','M','N'},
{'Q','A','T','I','T'}};
String pattern = "ACPOVFTAQWVXAC";
System.out.println(findPattern(m,incRows,incColumns,m.length,m.length,pattern.toCharArray()));
}
}
}
- anandkiran2007 April 03, 2016