Vipul Agarwal
BAN USERimport java.util.HashSet;
import java.util.Set;
public class Test {
public static void main(String[] args) {
String x ="String";
String y = "strong";
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < x.length(); i++) {
if(y.indexOf(x.charAt(i))!=-1){
set.add(x.charAt(i));
}
}
char[] xxx = new char[set.size()];
int i =0;
for (char xx : set) {
xxx[i] = xx;
i++;
}
String xxxx = new String(xxx);
System.out.println(xxxx);
}
}
import java.io.IOException;
import org.apache.commons.lang3.text.StrTokenizer;
public class CheckMetaString {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String x = "converse";
String y = "conserve";
int stratIndex = 0;
int lastIndex = 0;
boolean isflag = true;
if(x.length() != y.length())
throw new IOException("Strings are not meta Strings");
for (int i = 0; i < x.length(); i++) {
int value = x.charAt(i);
int value1 = y.charAt(i);
if(value != value1){
if(isflag){
stratIndex = x.indexOf(x.charAt(i));
isflag = false;
}
else {
lastIndex = x.indexOf(x.charAt(i));
}
}
}
char xx = x.charAt(stratIndex);
char yy = x.charAt(lastIndex);
char[] c = x.toCharArray();
c[stratIndex] = yy;
c[lastIndex] = xx;
String xxx = new String(c);
System.out.println(xxx);
if(xxx.equalsIgnoreCase(y)){
System.out.println("Strings are meta String");
}
}
}
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
public class CheckIsomorphic {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String s1= "abca";
String s2 = "xyzx";
Map<Character, Integer> map = new LinkedHashMap<Character, Integer>();
Map<Character, Integer> map1 = new LinkedHashMap<Character, Integer>();
if(s1.length()!=s2.length()){
throw new IOException("Strings are not Isomorphic");
}
else {
for (int i = 0; i < s1.length(); i++) {
if(!map.containsKey(s1.charAt(i))){
map.put(s1.charAt(i), 1);
}
else {
map.put(s1.charAt(i), map.get(s1.charAt(i))+1);
}
}
for (int i = 0; i < s2.length(); i++) {
if(!map1.containsKey(s2.charAt(i))){
map1.put(s2.charAt(i), 1);
}
else {
map1.put(s2.charAt(i), map1.get(s2.charAt(i))+1);
}
}
}
System.out.println(map);
System.out.println(map1);
List<Integer> list = new LinkedList<Integer>();
List<Integer> list1 = new LinkedList<Integer>();
for (Map.Entry<Character,Integer> x: map.entrySet()) {
list.add(x.getValue());
}
for (Map.Entry<Character,Integer> x: map1.entrySet()) {
list1.add(x.getValue());
}
System.out.println(list.equals(list1));
}
}
public class DuplicateNumberFrequency {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] x ={2,3,5,3,7,9,5,3,7};
Map<Integer,Integer> map = new LinkedHashMap<Integer,Integer>();
for (int i = 0; i < x.length; i++) {
if(!map.containsKey(x[i])){
map.put(x[i], 1);
}
else {
map.put(x[i], map.get(x[i])+1);
}
}
System.out.println(map);
List<Map.Entry<Integer, Integer>> list =
new LinkedList<Map.Entry<Integer, Integer>>(map.entrySet());
System.out.println(list);
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
});
System.out.println(list);
}
}
int arr[] = { 0, 1, 1, 1, 1, 0, 0, 1 };
int x =arr.length;
for (int i = 0; i < arr.length/2; i++) {
if(arr[i]>arr[x-1]){
int temp = arr[x-1];
arr[x-1] = arr[i];
arr[i] = temp;
}
x--;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
public void getMiddleElemnet(){
System.out.println();
ListNode curr = head;
ListNode curr1 = head;
while (curr!=null && curr.getNext() !=null) {
curr = curr.getNext().next;
curr1 = curr1.getNext();
}
System.out.println(curr1.getData());
}
public void getNthMinusyElement(int n){
System.out.println();
ListNode curr = head;
ListNode nthToLast = null;
int count = 0;
while(curr != null)
{
count++;
curr = curr.getNext();
if(count == n)
{
nthToLast = head;
}
else if (count > n)
{
nthToLast = nthToLast.getNext();
System.out.println("data==="+nthToLast.getData());
}
}
System.out.println(nthToLast.getData());
}
public class RotateArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] x = {10,20,30,40,50,60,70};
getRotateArray(x, 3);
}
public static void getRotateArray(int[]y, int rotate){
for (int j = 0; j < rotate; j++) {
for (int i = y.length-1; i > 0; i--) {
int temp =y[i];
y[i] =y[i-1];
y[i-1] =temp;
}
}
for (int i = 0; i < y.length; i++) {
System.out.println(y[i]);
}
}
}
public class TestPrime {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(checkPrime(17));
}
public static boolean checkPrime(int number){
boolean flag = true;
for (int i = 2; i <=number/2; i++) {
if(number% i ==0){
flag = false;
break;
}
}
return flag;
}
}
RepKinsleyJames, Network Engineer at Accenture
I graduated from College with a master’s degree in arthrogryposis. After graduation I am working as a manager in ...
Rephamishleeh, Blockchain Developer at ASAPInfosystemsPvtLtd
Property custodian with a reputed organization and help in keeping accurate check over the supplies and inventory of materials and ...
public class PrintDiamondShape {
- Vipul Agarwal May 24, 2017public static void main(String[] args) {
// TODO Auto-generated method stub
int num =9;
int l=1;
for (int i = 0; i <=num/2; i++) {
for (int j = i; j < num/2; j++) {
System.out.print(" ");
}
for (int k = 0; k <l; k++) {
System.out.print("*");
}
for (int j = i; j < num/2; j++) {
System.out.print(" ");
}
System.out.println();
l= l+2;
}
int x=0;
for (int i = 0; i <num/2; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
for (int k = 0; k <num-2-x; k++) {
System.out.print("*");
}
x=x+2;
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
System.out.println();
}
}
}