Java 8, 186 168168 167 bytes
s->{s=s.replaceAll("--|~~","");int l,i=(si=s.length()-(l=(s=s.replace("-~","")).length()))/2;return>>1;return(l-(s=s.replace("~","")).length()>0?"-"+s+~i:s+"+"+i).replace("--","");}
Try it online.-1 byte thanks to @ceilingcat replacing (a-b)/2
with a-b>>1
.
s->{ // Method with String as both parameter and return-type
s=s.replaceAll("--|~~",""); // Remove all "--" and "~~"
int l, // Temp integer to reduce bytes
i=(si=s.length()-(l=(s=s.replaceAll("-~","")).length()))/2;>>1;
// Remove all "-~" and save count in `i`
return(l-(s=s.replaceAll("~","")).length()
// Remove all remaining "~"
>0? // and if its non-empty:
"-" // Start with a minus sign
+s // followed by the remaining `s`
+~i // followed by `-i-1`
: // Else:
s // Start with the remaining `s`
+"+" // followed by a plus sign
+i // followed by `i`
).replaceAll("--","");}// And then remove all "--"