Navneel
BAN USERimport java.util.HashSet;
public class RemoveDuplicates {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(removeDuplicates("amazon development center", "aenr"));
}
public static char[] removeDuplicates(char[] source, char[] remove){
HashSet<Character> set = new HashSet<Character>();
for(int i=0 ; i < remove.length ; i++)
set.add(remove[i]);
int copyIndex =0;
for(int i=0 ; i < source.length ; i++){
if(!set.contains(source[i]))
source[copyIndex++] = source[i];
}
return new String(source).substring(0, copyIndex).toCharArray();
}
public static char[] removeDuplicates(String source, String remove){
if(source != null && remove != null)
return removeDuplicates(source.toCharArray(), remove.toCharArray());
if(source != null)
return source.toCharArray();
return null;
}
}
public class Compress {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(compress("ABCDEFFGHHHHKLLLLLLLLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMZ".toCharArray()));
System.out.println(compress("ABCDEFGHHNK".toCharArray()));
}
public static char[] compress(char[] str){
if(str==null || str.length<=1)
return str;
int copyIndex =0;
char prevChar = str[0];
int prevCharCount = 1;
//walk through the array
for(int i=1; i < str.length ; i++){
//check with previous char and current char
if(prevChar != str[i]){
//if prevChar count is only 1
if(prevCharCount==1){
str[copyIndex++] = prevChar;
prevChar = str[i];
}else{
str[copyIndex++] = prevChar;
prevChar = str[i];
char[] countInt = new Integer(prevCharCount).toString().toCharArray();
//copy the occurrence to the array
for(int j=0; j < countInt.length ; j++)
str[copyIndex++] = countInt[j];
}
//copy current one to prev char
prevChar = str[i];
prevCharCount = 1;
}else{
prevCharCount++;
}
}
//Check for the last char in the array
if(prevCharCount==1){
str[copyIndex++] = prevChar;
}else{
str[copyIndex++] = prevChar;
char[] countInt = new Integer(prevCharCount).toString().toCharArray();
//copy the occurrence to the array
for(int j=0; j < countInt.length ; j++)
str[copyIndex++] = countInt[j];
}
//return the compressed char array
return new String(str).substring(0, copyIndex).toCharArray();
}
}
public class MaxContinousRepeatChar {
- Navneel January 29, 2012/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getMaxContinuosRepeatedChar("1122333344556677777".toCharArray()));
}
public static char getMaxContinuosRepeatedChar(char[] ch){
if(ch == null || ch.length==0)
return '\0';
if(ch.length==1)
return ch[0];
char maxCh = ch[0];
int countMaxCh = 1;
char prevCh = ch[0];
int countPrevCh = 1;
for(int i=1; i < ch.length ; i++){
if(prevCh != ch[i]){
if(countMaxCh < countPrevCh){
maxCh = prevCh;
countMaxCh = countPrevCh;
}
prevCh = ch[i];
countPrevCh = 1;
}else{
countPrevCh++;
}
}
//for the last char
if(countMaxCh < countPrevCh){
maxCh = prevCh;
countMaxCh = countPrevCh;
}
return maxCh;
}
}