0
Help?
Hey , i am trying to do some challenges and i am doing pretty good. i am trying to figure out how to do a challenge that on the site and i cant make it. challenge: https://edabit.com/challenge/4hZ9cHgvkZ94sr2ae i started and got to here: (can you guys help?) public class Main{ static final String[] ALPHABET = {"a" , "b" , "c"}; public static void main (String[] args) { String[] arr = {"a" , "" , "c"}; } }
1 Antwort
+ 1
Something like this would be closer to what you want:
import java.util.*;
public class Main{
// join joins or concatenates all strings together into 1.
// For example, join(new String[] {"a", "", "c"}) returns "ac".
private static String join(String a[]) {
StringBuffer res = new StringBuffer();
for (String s: a) {
res.append(s);
}
return res.toString();
}
private static char missingLetter(String a[]) {
String s = join(a);
for (int i = 1; i < s.length(); i++) {
char expectedChar = (char)((int)s.charAt(0) + i);
if (expectedChar != s.charAt(i)) {
return expectedChar;
}
}
return '\0'; // indicate not found.
}
public static void main(String[] args){
System.out.println("Missing: " + missingLetter(new String[] {"a", "", "c"}));
}
}
These lines are an important aspect of it:
char expectedChar = (char)((int)s.charAt(0) + i);
if (expectedChar != s.charAt(i)) {
That does math with the ASCII value for each character so you don't need to define alphabetic sequences. You can just rely on the order characters are represented in ASCII tables like: http://www.asciitable.com/. Notice that the characters of the alphabet are sorted as you want.