16,003,873 members
1.00/5 (2 votes)
See more:
```Question —

|s| is the length of string s. Form a new string b as follows.

String b is initially empty.

For each i where 1 ≤ i ≤ |s|:

Append s[i-1] to the end of b.

Reverse the string b.

Before creating b, reorder s to produce the b that is the lexicographically maximum possible.

Example

s = "011".

Reorder s from "011" to "101". String b is formed as follows.

b after

--------------

Operation Append Reverse

--------- ------ -------

1 1 1

2 10 01

3 011 110

Return "101", the permutation of s that generates the maximum possible b.

Function Description

Create the custom function with parameter that accepts String.

Returns - string: the permutation of s that generates the maximal b

Sample Case 0

Sample Input For Custom Testing

STDIN FUNCTION

----- --------

1100 → s = "1100"

Sample Output

0101

Explanation

Using the permutation "0101":

b after

--------------

Operation Append Reverse

--------- ------ -------

1 0 0

2 01 10

3 100 001

4 0011 1100

Constraints

1 ≤ |s| ≤ 10^5

The string s consists only of characters ‘0’ and ‘1’.

I also have done the research and found the code that is similar to what I am looking for but it is in C++ and I don't understand it..

string getOptimalString(string s) {
string ans;
int n=s.length();
sort(s.begin(),s.end());
reverse(s.begin(),s.end());
if(n%2) ans.push_back(s[n/2]);
for(int i=(n+1)/2,j=(n-2)/2;j>=0 && i<n;j--,i++){
ans.push_back(s[i]);
ans.push_back(s[j]);
}
return ans;```

What I have tried:

```//The attempt I tried below:

public static String getOptimalString(String s) {
StringBuilder b = new StringBuilder();
int n = s.length();

for(int i = 0;i<n;i++){
if(s.charAt(i)%2 == 0){
b.append("0");
}else{
b.append("s.charAt(i)");}}
b = new StringBuilder(s).reverse();return b.toString();
}

public static void main(String[] args) throws IOException{
System.out.println(getOptimalString("1100"));
}
//From input "1100" the expected result should "0101" but I get "0011"... ```
Posted
Updated 6-Apr-23 5:12am
v2
Comments
Dave Kreskowiak 6-Apr-23 11:34am
Coding challenges are there to get YOU to think about the problem, not beg for someone else to think about it for you.

## Solution 1

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

## Solution 2

You have converted the C code that you found to Java. But the C code is the solution to a different question. Read your question again and look at the requirements:
```For each i where 1 ≤ i ≤ |s|:

Append s[i-1] to the end of b.```

So you need a loop tht iterates i from 1 to n-1, and move the characters as specified.
So work on that and print the result.

When that is working correctly go to the next step:
`Reverse the string b.`

And finally when both of the above are working implement the first part:
`Before creating b, reorder s to produce the b that is the lexicographically maximum possible.`

Comments
Nagima Zhanibekova 6-Apr-23 13:23pm
@Richard MacCutchan
Thank you for your response and help! I'm having hard time understanding the condition:
"For each i where 1 ≤ i ≤ |s|:

Append s[i-1] to the end of b.

So you need a loop tht iterates i from 1 to n-1, and move the characters as specified.
So work on that and print the result."

Do you mean something like:
for(int i=0;i<1 && i
Richard MacCutchan 6-Apr-23 13:52pm
No, as stated in the question you start at 1 and go on to length of s minus 1, so:
```for (int i = 1; i < n; i++)
{
b.append(s[i - 1];
}
```
Nagima Zhanibekova 6-Apr-23 15:40pm
So far, I have the created the below code for only 2 steps you mentioned, but I cannot understand the meaning of the 3rd step:"Before creating b, reorder s to produce the b that is the lexicographically maximum possible."

If possible were you able to re-phrase it?

public static String getOptimalString(String s) {
StringBuilder b = new StringBuilder();
int n = s.length();
for (int i = 1; i < n; i++)
{
b.append(s.charAt(i - 1));
}

b = new StringBuilder(s).reverse();

return b.toString();
}
Richard MacCutchan 7-Apr-23 3:32am
"but I cannot understand the meaning of the 3rd step"
Sorry, but I did not write this assignment. If you do not understand it then I suggest you ask your teacher for more details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard Deeming 65 Yvan Rodrigues 40 Pete O'Hanlon 30 Richard MacCutchan 10 OriginalGriff 10
 OriginalGriff 641 Richard Deeming 395 Pete O'Hanlon 300 Dave Kreskowiak 286 CPallini 120

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900