vandana.kcp
BAN USERWhile j is less than length of the char array keep advancing as long as there are whitespaces. Once we find non space character, shift them back to the ith location. Finally convert this to a string and return. It is O(n) solution where n is the length of the string.
public static String trimSpaces(String input) {
char[] inputChars = input.toCharArray();
int length = input.length();
int i = 0, j = 0;
while (j < length) {
while (j < length && inputChars[j] == ' ') {
j++;
}
while (j < length && inputChars[j] != ' ') {
inputChars[i++] = inputChars[j++];
}
inputChars[i++] = ' ';
}
while (i < length && inputChars[i] != ' ') {
inputChars[i++] = '\0';
}
String output = new String(inputChars);
return output;
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.ListIterator;
import java.util.Map;
public class BookPageSpeed {
static Map<String, Map<String, CustomerReadingSpeed>> bookReadingSpeed = new HashMap<String, Map<String, CustomerReadingSpeed>>();
static Map<String, Integer> maxBookReadingSpeed = new HashMap<String, Integer>();
static Map<String, ArrayList<ArrayList<String>>> leaderBoard = new HashMap<String, ArrayList<ArrayList<String>>>();
public static void addNewBook(String bookID){
bookReadingSpeed.put(bookID, null);
leaderBoard.put(bookID, null);
}
public static void addNewCustomer(String bookID, String customerID){
Map<String, CustomerReadingSpeed> custReadingSpeed;
CustomerReadingSpeed cust = new CustomerReadingSpeed();
if(bookReadingSpeed.get(bookID) == null){
custReadingSpeed = new HashMap<String, CustomerReadingSpeed>();
}
else{
custReadingSpeed = bookReadingSpeed.get(bookID);
}
custReadingSpeed.put(customerID, cust);
bookReadingSpeed.put(bookID, custReadingSpeed);
maxBookReadingSpeed.put(customerID, 0);
ArrayList<ArrayList<String>> custLeaderBoard;
if(leaderBoard.get(bookID) != null){
custLeaderBoard = leaderBoard.get(bookID);
}
else{
custLeaderBoard = new ArrayList<ArrayList<String>>(100);
for(int i=0; i<101; i++){
ArrayList<String> cust_i = new ArrayList<String>();
custLeaderBoard.add(cust_i);
}
}
custLeaderBoard.get(0).add(customerID);
leaderBoard.put(bookID, custLeaderBoard);
}
public static void updateReadingSpeeds(String customerID, String bookID, int pageNumber){
CustomerReadingSpeed custReadingSpeed = bookReadingSpeed.get(bookID).get(customerID);
custReadingSpeed.CustomerReadingPage.add(pageNumber);
int size= custReadingSpeed.CustomerReadingPage.size();
int speed = custReadingSpeed.CustomerReadingPage.get(size-1) - custReadingSpeed.CustomerReadingPage.get(size-2);
custReadingSpeed.CustomerReadingSpeed.add(speed);
int prev_speed = maxBookReadingSpeed.get(customerID);
if(speed > prev_speed){
leaderBoard.get(bookID).get(prev_speed).remove(customerID);
maxBookReadingSpeed.put(customerID, speed);
leaderBoard.get(bookID).get(speed).add(customerID);
}
}
public static void printLeaderboard(String bookID) {
int index = 100;
int rank =1;
System.out.print("CustomerID, Reading Speed, Rank\n");
ListIterator<ArrayList<String>> it = leaderBoard.get(bookID).listIterator(index);
while(it.hasPrevious()){
int readingSpeed = it.previousIndex();
ArrayList<String> customers = it.previous();
if(!customers.isEmpty()){
for(String custID: customers){
System.out.printf("Customer %s, %d, %d\n", custID, readingSpeed, rank);
}
rank++;
}
}
}
public static void main(String[] args){
addNewBook("A");
addNewCustomer("A", "A");
addNewCustomer("A", "B");
updateReadingSpeeds("A", "A", 2);
updateReadingSpeeds("B", "A", 5);
printLeaderboard("A");
}
}
import java.util.ArrayList;
public class CustomerReadingSpeed {
public ArrayList<Integer> CustomerReadingPage = new ArrayList<Integer>();
public ArrayList<Integer> CustomerReadingSpeed = new ArrayList<Integer>();
public CustomerReadingSpeed(){
CustomerReadingPage.add(0);
CustomerReadingSpeed.add(0);
}
}
First check if the charAt gives / at i , if yes check if i+1 contains * or another /. If yes put comment is true. And don't print any character folllowing that Else print it out. If you encounter * followed by /, put comment as false. Once reading a line is completed, we can set slash comment to be false.
- vandana.kcp July 01, 2014