-1

I have a java problem in which I have to see if a 9 digit number contains ONLY 1,2,3 - and also has the same number of 1 as 2 as 3 and vice versa.

For example:

  • 123231312 = true
  • 111111111 = false
  • 112731923 = false
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Bruce
  • 25
  • 2

4 Answers4

0
 private boolean onlyContainsOneTwoAndThree(int number) {
    char[] digitsFromNumber = String.valueOf(number).toCharArray(); // convert to char[] for easier manipulation.
    int oneCounter = 0;
    int twoCounter = 0;
    int threeCounter = 0;

    for (char digit : digitsFromNumber) {
        switch (digit) {
            case '1':
                oneCounter++;
                break;
            case '2':
                twoCounter++;
                break;
            case '3':
                threeCounter++;
                break;
            default:
                return false;
        }
    }
    if ((oneCounter == twoCounter) && (twoCounter == threeCounter)) {
        return true; // return true only when 1,2 and 3 appear exactly the same number of times.
    }
    return false;
}
Rahul Sharma
  • 892
  • 1
  • 6
  • 16
0
boolean correct(String input) {
    Map<Character, Integer> map = new HashMap<>();
    char[] charArray = input.toCharArray();

    for (char num : charArray) {
        Integer count = map.get(num);
        if (count == null) {
            count = 0;
        }
        map.put(num, count.intValue() + 1);
    }

    Integer val = map.get('1');
    if (val != null && val == map.get('2') && val == map.get('3') &&
        map.size() == 3) {
        return true;
    }

    return false;
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0
public static boolean isGood(String input) {
     return input.length() == 9 && 2D == input.chars()
                                            .parallel()
                                            .mapToObj(i -> (char)i)
                                            .map(Character::getNumericValue)
                                            .map(number -> number >= 1 && number <= 3 ? number : Integer.MAX_VALUE)
                                            .mapToInt(number -> number).average().orElse(-1);
}
David
  • 557
  • 4
  • 15
  • where did you find the method 'chars()'? – Rahul Sharma Mar 07 '16 at 01:58
  • After the filter, you will just have 1,2 and 3, that doesn't imply that the input string did not have other numbers (4,5,6 etc.). Try testing the code before posting. – Rahul Sharma Mar 07 '16 at 02:02
  • String.chars is available with Java 8 http://stackoverflow.com/questions/22435833/why-is-string-chars-a-stream-of-ints-in-java-8 – ecle Mar 07 '16 at 02:03
  • I don't think you know what filter means? Also the chars method is from the CharSequence interface. https://docs.oracle.com/javase/8/docs/api/java/lang/CharSequence.html#chars – David Mar 07 '16 at 02:04
  • dude, just try to run the method you suggested, with 123456789 as input. – Rahul Sharma Mar 07 '16 at 02:08
  • and filter just returns the stream of elements which satisfy the condition, which doesn't imply that other digits are not present in the input string. – Rahul Sharma Mar 07 '16 at 02:09
  • removed the filter. whoops. – David Mar 07 '16 at 02:27
-1
String s = Integer.toString(number);
System.out.println(s.contains("1") && s.contains("2") && s.contains("3"));
user455497
  • 637
  • 2
  • 6
  • 13
  • The OP specifically mentioned that `112731923 = BAD`, but your code would pass it, since it contains a 1, 2, and a 3. Please test your code before posting it. – azurefrog Mar 07 '16 at 01:03