hojoborolo.ultopalta
BAN USERpackage cup.google;
public class StringManipulation {
/*
Given two string check if they can be made equivalent by performing some operations on one or both string.
swapEven:swap a character at an even-numbered index with a character at another even-numbered index
swapOdd:swap a character at an odd-numbered index with a character at another odd-numbered index
Given : s="cdab" , x="abcd"
s -> cdab ->swap a and c ->adcb (swapEven)-> swap b and d (swapOdd) -> s="abcd" = x="abcd"
Given: s="dcba" , x="abcd"
no amount of operation will move character from an odd index to even index, so the two string will never be equals
Given: s="abcd" ,x="abcdcd"
x length to big so will never be equals
*/
public static void main(String[] args) {
System.out.println(manipuateStrings("dcba", "abcd"));
}
/* Logic
* for each character of first string,
* check if that char is in any even or odd position of the second string depending
* on the even or odd position of the
* first string char
*/
public static boolean manipuateStrings(String first, String second){
if(first==null || second ==null ||first.length()!=second.length()) return false;
char[] firstToChar = first.toCharArray();
char[] secondToChar = second.toCharArray();
boolean found;
for(int i=0;i<first.length() ;i++){
int j = (i%2==0)?0:1;
found = false;
for( ;j< second.length();j=j+2){
if(firstToChar[i] == secondToChar[j]){
found = true;
}
}
if(!found) return false;
}
return true;
}
}
package cup.google;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Set;
public class NearestBike {
/* You are given a campus map with the Google buildings, roads and Google
bikes. You have to help the employee find the nearest Google bike.
Campus map:
. - Free path/road
# - Building
B - Google bike
Employee location - (x, y) - (1, 2)
. . . . . #
. . E . . #
# # # # . #
. B . . . .
. . . . . B
*/
public static void main(String[] args) {
int[][] matrix = {{0,0,0,0,0,1},{0,0,0,0,0,1},{1,1,1,1,0,1},{0,2,0,0,0,0},{0,0,0,0,0,2}};
Point employee = new Point(1,2);
bfsBikeSearch(matrix, employee);
}
public static List<Point> getAdjacentPaths(int[][] matrix, Point start){
List<Point> adjacnetPaths = new ArrayList<NearestBike.Point>();
if(start.x-1>=0 && matrix[start.x-1][start.y] !=1) adjacnetPaths.add(new Point(start.x-1,start.y));
if(start.x+1 < matrix.length && matrix[start.x+1][start.y] !=1) adjacnetPaths.add(new Point(start.x+1,start.y));
if(start.y-1 >=0 && matrix[start.x][start.y-1] !=1 ) adjacnetPaths.add(new Point(start.x,start.y-1));
if(start.y+1 < matrix[0].length && matrix[start.x][start.y+1] !=1) adjacnetPaths.add(new Point(start.x,start.y+1));
return adjacnetPaths;
}
public static void bfsBikeSearch(int[][] matrix, Point employee){
if(matrix[employee.x][employee.y] == 2) {
System.out.println("Fount bike at employee location (" + employee.x + "," + employee.y + ")"); return;
}
Queue<Point> queue = new LinkedList<Point>();
Set<Point> visited = new HashSet<NearestBike.Point>();
visited.add(employee);
queue.add(employee);
while(!queue.isEmpty()){
Point loc = queue.remove();
List<Point> neighbors = getAdjacentPaths(matrix, loc);
for(Point neighbor : neighbors){
if(!visited.contains(neighbor)){
if(matrix[neighbor.x][neighbor.y] == 2){
System.out.println("Fount bike at employee location (" + neighbor.x + "," + neighbor.y + ")"); return;
}else{
visited.add(neighbor);
queue.add(neighbor);
}
}
}
}
}
static class Point{
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + x;
result = prime * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
int x;
int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
public Point(){
}
}
}
package cup.salesforce;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PrintSequence {
public static void main(String[] args) {
Printer printer = new Printer();
PrinterThread one = new PrinterThread(printer, 1);
PrinterThread two = new PrinterThread(printer, 2);
PrinterThread three = new PrinterThread(printer, 3);
one.start();
two.start();
three.start();
}
static class Printer{
int prevId;
Lock lock = new ReentrantLock();
Condition[] conditions = {lock.newCondition(), lock.newCondition(), lock.newCondition()};
public Printer(){
this.prevId = 0;
}
public void print(int id){
lock.lock();
try{
if(prevId!=id-1)conditions[id-1].await();
System.out.println(" " + id);
int nxtId = id > 2 ? 0:id;
this.prevId = id;
conditions[nxtId].signal();
}catch(InterruptedException e){
e.printStackTrace();
}finally{
lock.unlock();
}
}
}
static class PrinterThread extends Thread implements Runnable{
Printer printer;
int id;
public PrinterThread(Printer printer, int id){
this.printer = printer;
this.id = id;
}
public void run(){
while(true) printer.print(this.id);
}
}
}
- hojoborolo.ultopalta November 17, 2018