hm
BAN USERJava code of the solution above
package career.cup.yelp;
import java.awt.List;
import java.util.ArrayList;
import java.util.HashMap;
public class findResturant {
public static HashMap<String, ArrayList<String>> restMenuList= new HashMap <String, ArrayList<String>>();
public static void main(String args[]) {
populateResturantMenus();
System.out.println(restMenuList);
findItemOnMenus("Burger");
findItemOnMenus("Potato Chips");
}
public static void findItemOnMenus(String foodItem) {
System.out.println(restMenuList.get(foodItem));
}
public static void populateResturantMenus()
{
String[] americanMenu ={"Burger", "French fries", "Potato Chips"};
for(String menu: americanMenu){
if(restMenuList.containsKey(menu)){
ArrayList<String> restList = restMenuList.get(menu);
restList.add("American");
restMenuList.put(menu, restList);
} else {
ArrayList<String> restList = new ArrayList<String>();
restList.add("American");
restMenuList.put(menu,restList);
}
}
String[] italianMenu ={"Pizza", "Sticks", "Potato Chips"};
for(String menu: italianMenu){
if(restMenuList.containsKey(menu)){
ArrayList<String> restList = restMenuList.get(menu);
restList.add("Italian");
restMenuList.put(menu, restList);
} else {
ArrayList<String> restList = new ArrayList<String>();
restList.add("Italian");
restMenuList.put(menu,restList);
}
}
}
}
We can use a stack for this problem.
Step 1: If destination doesn't start with root, enter source directories to stack
Step 2: Split source and destination around match of regular expression "/" and put each of them in array.
Step 3: If a ".." pattern is found, pop the last added directory from array, otherwise push it to array.
Step 4: to get the end directory, look at the topmost element of the array
package career.cup.yelp;
import java.util.ArrayList;
import java.util.Stack;
public class getDirectory {
public static void main(String args[]){
getDirectiry("/logs", "test/nodes/../modules/logs");
getDirectiry("/logs", "test/nodes/../modules/logs/..");
}
public static void getDirectiry(String src, String destination){
Stack<String> directoryStack = new Stack<String>();
String[] listDestination = destination.split("/");
String[] listHost = src.split("/");
if(!destination.startsWith("/")){
for(String str : listHost){
if(!(str.matches(".."))){
directoryStack.push(str);
}
else if((str.matches(".."))){
directoryStack.pop();
}
}
}
for(String str : listDestination){
if(!(str.matches(".."))){
directoryStack.push(str);
}
else if((str.matches(".."))){
directoryStack.pop();
}
}
System.out.println(directoryStack);
}
}
Step 1: Maintain a 256 integer array to store position of last visited character.
Step 2: start from beginning. Check last visited position of char in array.
Step 3: if position is less than the start it means the char is not found in current substring so continue
Step 4: Is position is more than start from there
private static void longestSubstr(String string) {
// TODO Auto-generated method stub
if(string == null || string.trim().length() <=0){
return;
}
Queue<Integer> lenQ = new LinkedList();
int[] placeSet = new int[256];
int start =0;
int len =1;
for(int i=0;i<string.length();i++){
if(placeSet[string.charAt(i)] < start) {
placeSet[string.charAt(i)] =i;
} else {
start = placeSet[string.charAt(i)]+1;
placeSet[string.charAt(i)] = i;
}
if((i-start+1) > len) {
len = i-start+1;
lenQ.clear();
lenQ.add(start);
} else if((i-start+1) == len){
lenQ.add(start);
}
}
while(!lenQ.isEmpty()){
int startStr = lenQ.remove();
System.out.println(" longest String : "+startStr +" length "+len);
for(int i=0;i<len;i++)
System.out.print(string.charAt(startStr+i));
}
return ;
}
I did not browse all but most of the solutions are creating a separate string. Please find below in place solution
public class compressString {
private static String compressedString(String string) {
// TODO Auto-generated method stub
if(string == null){
return null;
}
char strCharArr[] = string.toCharArray();
int j=0;
int ptr=0;
int count=0;
for(int i=0;i<string.length();i++){
if(string.charAt(j)==string.charAt(i)) {
count++;
} else {
if(count > 1) {
strCharArr[ptr++] = Integer.toString(count).charAt(0);
}
count =1;
j=i;
}
if(count ==1) {
strCharArr[ptr++] = strCharArr[i];
}
}
while(ptr < strCharArr.length)
strCharArr[ptr++]='\0';
return String.valueOf(strCharArr);
}
}
I did not browse all but most of the solutions are creating a separate string. Please find below in place solution
public class compressString {
private static String compressedString(String string) {
// TODO Auto-generated method stub
if(string == null){
return null;
}
char strCharArr[] = string.toCharArray();
int j=0;
int ptr=0;
int count=0;
for(int i=0;i<string.length();i++){
if(string.charAt(j)==string.charAt(i)) {
count++;
} else {
if(count > 1) {
strCharArr[ptr++] = Integer.toString(count).charAt(0);
}
count =1;
j=i;
}
if(count ==1) {
strCharArr[ptr++] = strCharArr[i];
}
}
while(ptr < strCharArr.length)
strCharArr[ptr++]='\0';
return String.valueOf(strCharArr);
}
}
There could be error due to following reasons
1. The configuration of Jenkins has something causing the error. Could be permgen space, maven version, Java version, e.t.c
2. Jenkins needs a wipe out to clear cached dependencies.
3. Test case is referring some local path or directory Jenkin is not aware of.
4. Local or test environment has a version of dependency cached that is making in work.
Use Stack.
Push into the stack until you a get a closing brace.
On getting a closing brace, pop out an element from the stack.
If (you got anything but a brace continue popping until you get a brace)
check the brace is a matching one else return "wrong string"
continue with the string
else(if you got a brace on first pop) it would be an extra brace
return "wrong string"
private static boolean checkBracesSequence(String string) {
Stack st = new Stack();
for(char c: string.toCharArray()) {
if( c != ')' && c != '}' && c!= ']') {
st.push(c);
}
else {
int count =0;
while(!st.isEmpty()) {
char t = st.pop().toString().charAt(0);
if(t =='(' || t == '{' || t == '[') {
if(count == 0) {
System.out.println("Extra Brackets");
return false;
}
if(c == ')' && t == '(') {
break;
} else if(c=='}' && t=='{') {
break;
} else if (c==']' && t=='[') {
break;
}
}
count++;
}
}
}
while(!st.isEmpty()) {
char t = st.pop().toString().charAt(0);
if(t =='(' || t == '{' || t == '['){
return false;
}
}
return true;
}
package career.cup.bloomberg;
public class stringCmp {
public static void main(String args[]) {
System.out.println("String Compare "+strLocalCmp("Hello","Hello"));
}
private static boolean strLocalCmp(String string, String string2) {
// TODO Auto-generated method stub
if(string==null || string2 == null){
return false;
}
if(string.length() != string2.length()) {
return false;
}
for(int i=0;i<string.length();i++){
if(string.charAt(i) != string2.charAt(i)) {
return false;
}
}
return true;
}
}
Here is the Stack implementation of Polish Notation Calculator. Logic runs as below
1. Split the given string into a string array
2. Start from the back of the array
3. If you find braces "(" or ")" just ignore
4. If you find an operand, push onto stack
5. If you find an operator, pop two top operands, calculate result and then push back the result to stack.
Implementation goes below
- hm February 12, 2015