Google Interview Question
SDE1sCountry: United States
public static void main (String[] args) throws java.lang.Exception
{
//String str = "C6H6((CH2)6(NO2)3)4";
String str = "C6H6(CH2)6(NO2)3";
Map<Character,Integer> map = new HashMap<>();
map = createChemicalMap(str);
for(Character c : map.keySet()){
System.out.println(c+" : " + map.get(c));
}
}
public static Map<Character,Integer> createChemicalMap(String str){
Map<Character,Integer> map = new HashMap<>();
if(str == null || str.length()<1){
return map;
}
char[] array= str.toCharArray();
Deque<Map<Character,Integer>> deque = new ArrayDeque<>();
int num = 0;
for(int i=0;i<array.length;i++){
if(Character.isDigit(array[i])){
char pre_c = array[i-1];
num = array[i] - '0';
while(i+1<array.length && Character.isDigit(array[i+1])){
num = num*10 + array[i+1]-'0';
i++;
}
if(pre_c!=')'){
map.put(pre_c,map.get(pre_c)*num);
}else{
for(char c : map.keySet()){
map.put(c,map.get(c)*num);
}
if(!deque.isEmpty()){
Map<Character,Integer> pre_map = deque.pollLast();
for(Character c : map.keySet()){
pre_map.put(c,pre_map.getOrDefault(c,0)+map.get(c));
}
map = pre_map;
}
}
}else if(array[i] == '('){
deque.offer(map);
map = new HashMap<>();
}else if(array[i]!=')'){
if(!map.containsKey(array[i])){
map.put(array[i],1);
}
}
}
return map;
}
public static void main (String[] args) throws java.lang.Exception
{
String str = "C6H6(CH2H)6(NO2)3";
Map<Character,Integer> map = new HashMap<>();
map = createChemicalMap(str);
for(Character c : map.keySet()){
System.out.println(c+" : " + map.get(c));
}
}
public static Map<Character,Integer> createChemicalMap(String str){
Map<Character,Integer> map = new HashMap<>();
if(str == null || str.length()<1){
return map;
}
char[] array= str.toCharArray();
Deque<Map<Character,Integer>> deque = new ArrayDeque<>();
int num = 0;
for(int i=0;i<array.length;i++){
if(Character.isDigit(array[i])){
char pre_c = array[i-1];
num = array[i] - '0';
while(i+1<array.length && Character.isDigit(array[i+1])){
num = num*10 + array[i+1]-'0';
i++;
}
if(pre_c!=')'){
map.put(pre_c,map.get(pre_c)+num-1);
}else{
for(char c : map.keySet()){
map.put(c,map.get(c)*num);
}
if(!deque.isEmpty()){
Map<Character,Integer> pre_map = deque.pollLast();
for(Character c : map.keySet()){
pre_map.put(c,pre_map.getOrDefault(c,0)+map.get(c));
}
map = pre_map;
}
}
}else if(array[i] == '('){
deque.offer(map);
map = new HashMap<>();
}else if(array[i]!=')'){
map.put(array[i],1+map.getOrDefault(array[i],0));
}
}
return map;
}
public static void main(String args[]) {
String chemicals = "C6CCH2(N021)H20";
HashMap<Character, Integer> result2 = getcount(chemicals);
System.out.println("Chemical hashmap: " + result2);
}
public static HashMap<Character, Integer> getcount(String chemicals){
HashMap<Character, Integer> hmap = new HashMap<Character, Integer>();
int value = 0;
for(int i = 0; i < chemicals.length(); i++) {
char key = chemicals.charAt(i);
if(hmap.containsKey(key)){
value = hmap.get(key);
hmap.put(key, ++value);
}else {
hmap.put(key, 1);
}
}
System.out.println("hmap: " + hmap.toString());
return hmap;
}
- divm01986 April 12, 2017