1

I have a string that has three letters, 1, 2, X. Sometimes the string can be 12, or 1X, or 2X..etc.

I would like to append a "-" based on the position of the input letters. For example If I get 1, the result should be 1--, If I get X, the results should --X, If I get 2X, the result should be -2X.

My approach is to use a state machine of 2^n selections, but I'm looking for a smart apporach and better apporach.

More examples

Input 1    2    X  2X
Ouput 1-- -2- --X -2X
3
  • 2
    Please show your current code, what you've tried. Commented Aug 5, 2013 at 11:36
  • You wanna have an order if not all 3 letters are used? So the natural order is 12X or is X21 etc. also possible? Commented Aug 5, 2013 at 11:37
  • Pleae give more examples of correct inputs and outputs. Commented Aug 5, 2013 at 11:38

3 Answers 3

6

You can check if the input string contains the characters using String.contains():

String in = "12X";

String out;

out  = in.contains("1") ? "1" : "-";
out += in.contains("2") ? "2" : "-";
out += in.contains("X") ? "X" : "-";

Which will produce your desired results:

(in)    (out)
1       1--
12      12-
1X      1-X
X21     12X
X       --X

As pointed out in the comments, this will be more efficient:

String in = "12X";

String out;

out  = ( in.contains("1") ? "1" : "-" )
     + ( in.contains("2") ? "2" : "-" )
     + ( in.contains("X") ? "X" : "-" );
Sign up to request clarification or add additional context in comments.

7 Comments

That was a typo, fixed.
Concatenating Strings is not a good idea, since a lot of useless objects are created.
I do not think that really matters here. If Mahmoud wanted to have super-efficient code, he should've used C instead.
I would at least rewrite this to a single concatenation expression. Almost the same code, but uses a single StringBuilder behind the scenes. Sure, for such a toy example it doesn't matter, but there's much to learning and upholding the right practices, so you hit the wall of insufficient resources only when the problem is intrinsically tough.
Yes, that's it. This reduces the complexity of string building from O(n^2) to O(n).
|
3

This is one approach. Its advantage is that the size of code is constant with respect to the number of supported symbols.

final String x = "12X";
final char[] out = new char[x.length()];
Arrays.fill(out, '-');
for (int i = 0; i < input.length(); i++) {
   final char c = input.charAt(i);
   out[x.indexOf(c)] = c;
}
System.out.println(new String(out));

4 Comments

@MightyPork It may be the hard way for you, but it was the most obvious way for me. Different minds think differently.
@MarkoTopolnik I think it should be out[x.indexOf(c)] = c;, unless I am mistaken.
x.indexof, will not make it constant though.
@Mahmoud Note that I said "the size of code is constant", not the runtime complexity of the code.
1
public static String getStringWithDashes(String input)
{
    final char result[] = "---";
    if (input.contains("1"))
    {
        result[0] == '1';
    }
    if (input.contains("2"))
    {
        result[1] == '2';
    }
    if (input.contains("X"))
    {
        result[2] == 'X';
    }
    return new String(result);
}

5 Comments

Yep, but there is no 3
If you use inline IF's, it will be better readable and less bulgy.
@MightyPork Just a matter of coding conventions.
I don't say this is wrong. Only that it's hard to read and will be very long if you want to add more characters.
@MightyPork This is hard to read? ;) With more characters I would go with a loop, like Marko Topolnik did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.