I wrote this code for the Self Describing Number problem as mentioned here. My code is quite simple (compared to other solutions online), in my opinion, which makes me question the validity of it (and the runtime, which I think is just O(N)). Any feedback on the solution would be much appreciated!
private int[] countsNeeded;
private int[] countsFound;
private static boolean isSelfDescribed(int n) {
countsNeeded = new int[10];
countsFound = new int[10];
String s = Integer.toString(n);
for (int i = 0; i < s.length(); i++) {
int digit = Integer.parseInt(s.charAt(i) + "");
countsNeeded[i] = digit;
countsFound[digit]++;
}
for (int i = 0; i < 10; i++) {
if (countsNeeded[i] != countsFound[i]) {
return false;
}
}
return true;
} I seem to be getting the right output when testing with some values, but the simplicity of just storing the target count and the counts found in two arrays seems too easy?