static String replace_and_remove(String s) {
		char[] s_chars = s.toCharArray();
	  // Removes "b" and count the number of "a".
	  int write_idx = 0, a_count = 0;
	  for (char c : s_chars) {
	    if (c != 'b') {
	      s_chars[write_idx++] = c;
	    }
	    if (c == 'a') {
	      ++a_count;
	    }
	  }

	  // Allocates space according to the number of "a".
	  s_chars = Arrays.copyOf(s_chars, write_idx + a_count);
	  // Replace "a" with "dd".
	  int cur_idx = write_idx - 1;
	  write_idx = s_chars.length - 1;
	  while (cur_idx >= 0) {
	    if (s_chars[cur_idx] == 'a') {
	      s_chars[write_idx--] = 'd';
	      s_chars[write_idx--] = 'd';
	    } else {
	      s_chars[write_idx--] = s_chars[cur_idx];
	    }
	    --cur_idx;
	  }
	  return new String(s_chars);
	}