vijay.lokhande.in
BAN USERvoid reapitedMaxChars(String str){
char maxchar=str.charAt(0);
int maxcount=1;
for (int i = 0; i < str.length(); i++) {
char tmp=str.charAt(i);
int count=1;
for (int j = i+1; j < str.length(); j++) {
if(tmp == str.charAt(j)){
count++;
}
else{
if(maxchar == tmp){
if(maxcount < count){
maxcount=count;
}
}
else{
if(maxcount < count){
maxchar=tmp;
}
}
i=j;
break;
}
}
}
System.out.println(maxchar +" -> "+maxcount);
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ds.linklist.op;
/**
*
* @author lokhavij
*/
public class SingleLinkList {
Node start;
Node current;
int length;
public void add(int data){
Node node;
if(start==null){
start=new Node(data);
current=start;
length=1;
}
else{
current.next=new Node(data);
current=current.next;
length++;
}
}
public void delete(int data){
Node item=start;
if(item.data==data){
start=item.next;
}
else{
while(item!=null) {
if(item.next.data==data){
item.next=item.next.next;
length--;
break;
}
item=item.next;
}
}
}
public void print(){
Node item=start;
while (item!=null) {
System.out.println(item.data);
item=item.next;
}
}
public int getCount(){
return length;
}
public void reverse(){
Node prevTmp=null;
Node nextTmp=null;
Node headTmp=start;
while(headTmp!=null){
nextTmp=headTmp;
headTmp=headTmp.next;
nextTmp.next=prevTmp;
prevTmp=nextTmp;
start=nextTmp;
}
}
public static void main(String[] args) {
SingleLinkList linkList=new SingleLinkList();
linkList.add(0);
linkList.add(1);
linkList.add(2);
linkList.add(3);
linkList.add(4);
linkList.add(5);
linkList.print();
System.out.println("length ->"+linkList.getCount());
linkList.reverse();
linkList.print();
}
}
package com.engineeringkida.puzzle;
/*
* Given two sorted linked lists of integers
* write an algorithm to merge the two linked lists such that the resulting linked list is in sorted order.
* You are expected to define the data structure for linked list as well. Analyze the time and space complexity of the merge algorithm.
*
*/
public class Problem6 {
public void addElement(){
System.out.println("---------------------list 1-------------------");
Node start=null;
start=addElement(start,1);
start=addElement(start,11);
start=addElement(start,3);
start=addElement(start,22);
start=addElement(start,6);
start=addElement(start,35);
start=addElement(start,24);
printelment(start);
System.out.println("---------------------list 2-------------------");
Node start1=null;
start1=addElement(start1,11);
start1=addElement(start1,12);
start1=addElement(start1,4);
start1=addElement(start1,7);
start1=addElement(start1,2);
start1=addElement(start1,15);
start1=addElement(start1,14);
printelment(start1);
System.out.println("--------------------list1 + list2 --------------------");
meargeList(start, start1);
}
public Node addElement(Node start, int data){
if(start==null){
start=new Node(data);
}
else if(data < start.getData()){
Node node=new Node(data);
node.setNext(start);
start=node;
}
else {
Node current=start;
Node prev=null;
while(current!=null){
if(current.getData() < data){
prev=current;
current=current.getNext();
}
else if(current.getData() > data && prev.getData() < data){
Node tmp=new Node(data);
tmp.setNext(current);
prev.setNext(tmp);
break;
}
else if(current.getData()==data){
Node tmp=new Node(data);
tmp.setNext(current.getNext());
current.setNext(tmp);
break;
}
}
if(current==null){
prev.setNext(new Node(data));
}
}
return start;
}
public void printelment(Node start){
Node current=start;
while(current!=null){
System.out.println(current.getData());
current=current.getNext();
}
}
public void meargeList(Node list1,Node list2){
Node current=list2;
while(current!=null){
list1=addElement(list1, current.getData());
current=current.getNext();
}
printelment(list1);
}
class Node {
int data;
Node next;
Node(int data) {
this.setData(data);
this.setNext(null);
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
public static void main(String ar[]){
Problem6 problem6=new Problem6();
problem6.addElement();
}
}
public int numberOfKs(int k,int n){
int count=0;
for(int i=k;i<=n;i++){
if(i==k){
count++;
}
else{
int tmpNum=i;
while(tmpNum!=0){
int rem=tmpNum%10;
tmpNum=tmpNum/10;
if(rem==k){
count++;
}
}
}
}
return count;
}
public void spiralMatrix(int mat[][],int r,int c){
int r1=0,r2=r-1,c1=0,c2=c-1;
int visit=r*c,k=0;
while(k<visit){
for(int i=c1;i<=c2;i++){
System.out.println(mat[c1][i]);
k++;
}
for(int i=(r1+1);i<=r2;i++){
System.out.println(mat[i][r2]);
k++;
}
for(int i=c2-1;i>=c1;i--){
System.out.println(mat[c2][i]);
k++;
}
for(int i=(r2-1);i>=(r1+1);i--){
System.out.println(mat[i][r1]);
k++;
}
c1++;
c2--;
r1++;
r2--;
}
}
public class Problem3 {
public HashMap<Integer,Integer> sortCount(int array[]){
HashMap<Integer,Integer>numInfo=new HashMap<Integer,Integer>();
for(int i:array){
if(numInfo.containsKey(i)){
int count=numInfo.get(i);
count++;
numInfo.remove(i);
numInfo.put(i,count);
}
else if(numInfo.isEmpty() || numInfo.containsKey(i)==false){
numInfo.put(i, 1);
}
}
return numInfo;
}
public static void main(String ... ar){
Problem3 problem3=new Problem3();
int []array={1,2,4,3,1,2,4,5,3,2,1,0,3,4,5};
HashMap<Integer, Integer>nums=problem3.sortCount(array);
for(Map.Entry<Integer, Integer>item:nums.entrySet()){
System.out.println(item.getKey()+" : "+item.getValue());
}
}
}
String reverseWords(String inStr){
String array[]=inStr.split(" ");
String outStr="";
for(int i=(array.length-1);i>=0;i--){
outStr+=array[i]+" ";
}
outStr=outStr.trim();
return outStr;
}
public class Cup {
public static void main(String[] args) {
int []arrayIn={0,1,3,4,5,6,7};
int []arrayOut;
int n=4;
arrayOut=new Cup().reverseNum(arrayIn,n);
for(int i=0;i<arrayOut.length;i++){
System.out.println(arrayOut[i]);
}
}
public int[]reverseNum(int []arrayIn,int n){
int len=arrayIn.length;
int []arrayOut=new int[len];
for(int i=n,j=0;i<len;i++,j++){
arrayOut[j]=arrayIn[i];
}
arrayOut[n-1]=arrayIn[n-1];
for(int i=0,j=n;i<n-1;i++,j++){
arrayOut[j]=arrayIn[i];
}
return arrayOut;
}
}
package designpattern;
- vijay.lokhande.in September 20, 2017public class DoubleTon {
final static private DoubleTon instance1=new DoubleTon();
final static private DoubleTon instance2=new DoubleTon();
static private int counter=0;
private DoubleTon(){
}
static public DoubleTon getInstance(){
counter++;
if(counter%2==0){
System.out.println("instance1");
return instance1;
}
return instance2;
}
}