sk
BAN USERYou can do it using simple regex expression. As I see, there is no need of stacks. Sorry for my above post.
public class SimplifyUnixPath {
static String inppath;
public static void main(String[] args) {
String inputstring = "/home/abc/.././def/./ghi/../.";
//System.out.println(inputstring);
String toprint = simplifyPath(inputstring);
System.out.println(toprint);
// TODO Auto-generated method stub
}
public static String simplifyPath(String inp){
inppath = inp;
String splitexp = "/\\w*/\\.\\.|/\\.";
String output = "";
String[] outputarr;
outputarr = inppath.split(splitexp);
for(String s : outputarr){
//System.out.println(s);
output = output+s;
}
return output;
}
}
Turned out to be simpler than I thought. Hope I am not missing something. Here is the code in java:
public class Matrixspiral {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] matrixarr = new int[5][4];
matrixarr[0] = new int[]{3,2,1,0};
matrixarr[1] = new int[]{7,-2,3,9};
matrixarr[2] = new int[]{8,4,-9,2};
matrixarr[3] = new int[]{0,-3,-4,-5};
matrixarr[4] = new int[]{9,5,3,1};
printRound(matrixarr,0,matrixarr[0].length-1,matrixarr.length-1,0);
}
private static void printRound(int[][] inparr, int frow,int lcol, int lrow, int fcol){
for(int j=fcol;j<=lcol;j++){
System.out.println(inparr[frow][j]);
}
for(int i=frow+1;i <=lrow;i++){
System.out.println(inparr[i][lcol]);
}
for(int j = lcol-1;j >=fcol;j--){
System.out.println(inparr[lrow][j]);
}
for(int i=lrow-1;i >=frow+1;i--){
System.out.println(inparr[i][fcol]);
}
if(lcol-1 < 0 || lrow-1 <0)
return;
printRound(inparr,frow+1,lcol-1,lrow-1,fcol+1);
}
}
Not sure if this is an efficient way to do it. Just used a HashMap. Please suggest if there are better ways.
import java.util.*;
public class GroupAnagrams {
public static void main(String[] args){
String[] arrwords = {"art","tar","top","pot","neat","rat","tape","nate","peat","random"};
String[] srtdarrwords = sortAlphabetsInEachWord(arrwords);
HashMap<String, String> wordmap = new HashMap<String,String>();
int i=0;
for(String s : srtdarrwords){
System.out.println(s);
if(wordmap.containsKey(s)){
wordmap.put(s,wordmap.get(s)+","+arrwords[i++]);
System.out.println("already contains, so adding");
System.out.println(wordmap.get(s));
}
else{
wordmap.put(s,arrwords[i++]);
}
}
for(String s : wordmap.keySet()){
System.out.println("["+wordmap.get(s)+"]");
}
}
private static String[] sortAlphabetsInEachWord(String[] inparr){
String[] retstr = new String[inparr.length];
int i=0;
for (String s : inparr){
retstr[i++] = s;
}
i=0;
for (String s : retstr){
char[] alphword = s.toCharArray();
Arrays.sort(alphword);
s = new String(alphword);
retstr[i++] = s;
}
return retstr;
}
}
Algorithm:
1) Find occurrence of the key using binary search
2) As soon as you find this occurrence, mark the last array (indices) where you found this first occurrence > Let us call this a subarray. To make it clear, subarray is the array that we found the key, that means subarray[mid] = key.
3) No do again a binary search in the first half of this array to find the first occurrence of the key.
4) Do a binary search, similar to #3 to find the last index.
Here is the code in java:
public class FirstAndLastIndex {
private static int intr_f_index,intr_l_index,key;
public static void main(String args[]){
int[] inparray = {1,2,4,6,6,6,6,6,6,7,12,14,14,14,14,14,14,14,14,14,14,14,14,300};
int index,firstindex,lastindex,arrfindex,arrlindex,n;
key = 14;
n = inparray.length;
arrfindex = 0;
arrlindex = n-1;
index = binarySearchIndex(inparray,arrfindex,arrlindex);
if(index == -1){
firstindex =-1;
lastindex =-1;
System.out.println("{"+firstindex+","+lastindex+"}");
}
else{
firstindex = binarySearchFirstIndex(inparray,intr_f_index,index);
lastindex = binarySearchLastIndex(inparray,index,intr_l_index);
System.out.println("{"+firstindex+","+lastindex+"}");
}
}
private static int binarySearchIndex(int[] inparr, int findex, int lindex){
int mid = findex+(lindex-findex)/2;
if(findex == lindex && inparr[findex]!=key)
return -1;
if(inparr[mid] == key){
intr_f_index=findex;
intr_l_index=lindex;
return mid;
}
else if(inparr[mid] > key){
return binarySearchIndex(inparr,findex,mid-1);
}
else{
return binarySearchIndex(inparr,mid+1,lindex);
}
//return -1;
}
private static int binarySearchFirstIndex(int[] inparr,int findex,int lindex){
if(findex == lindex)
return findex;
int mid = findex+(lindex-findex)/2;
if(inparr[mid] == key){
return binarySearchFirstIndex(inparr,findex,mid);
}
else{
return binarySearchFirstIndex(inparr,mid+1,lindex);
}
}
private static int binarySearchLastIndex(int[] inparr, int findex, int lindex){
int mid;
if(findex == lindex)
return findex;
mid = findex+(lindex-findex)/2+1;
if(inparr[mid] == key){
return binarySearchLastIndex(inparr,mid,lindex);
}
else{
return binarySearchLastIndex(inparr,findex,mid-1);
}
}
}
@Loler: You mean to maintain the difference in count in a HashMap?
- sk August 11, 2013