if (text.length() < i * (i + 1) / 2 + i + 1) {
System.out.println("Falsch");
return;
}
public static boolean wort(String s) {
int n = (int) (Math.sqrt(8 * s.length() + 1) - 1) / 2;
if ((n * n + n) / 2 != s.length()) {
return false;
}
for (int i = 0, step = 1; i < s.length(); i += step, step++) {
char c = s.charAt(i);
for (int j = 1; j < step; j++) {
if (c != s.charAt(i + j)) {
return false;
}
}
}
return true;
}
Also ich würde auch am Anfang überprüfen, ob die Länge auch stimmt.
Ob es übersichtlicher ist... Drei Fragezeichen. : D
s.charAt(i-1) != s.charAt(i)
liegt.public static boolean wort(String s) {
if (s == null || s.isEmpty()) {
return false;
}
int[] edges = new int[s.length() + 1];
int length = 0;
for (int i = 0; i <= s.length(); i++) {
if (i == 0 || i == s.length() || s.charAt(i) != s.charAt(i - 1)) {
edges[length++] = i;
}
}
for (int i = 1, step = 1; i < length; i++, step++) { // i == step -> also kann das Eine oder Andere weggelassen werden, aber zwecks Übersichtlichkeit
int diff = edges[i] - edges[i - 1];
if (diff != step) {
return false;
}
}
return true;
}
boolean wort(String text){
char[] chars = text.toCharArray();
int i = 0, j = 0;
for(int k = 1; i < chars.length-1; i++){
if((chars[i] == chars[i+1])^(i == j ? (j+=++k) < 0 : true)){
return false;
}
}
return i == j;
}
public static void main(String[] args) {
String text = "abbcccddddeeeeeffffff";
String text2 = text.toUpperCase();
System.out.println(schnaps(text));
System.out.println(schnaps(text2));
}
static boolean schnaps(String text) {
try {
for (int i = 0, j = 1; i < text.length(); i += j, j++) {
System.out.println(text.substring(i, i + j)); // just an output ....
char befor = i > 0 ? text.charAt(i - 1) : (char) (text.charAt(i) - 1);
char after = text.charAt(i);
if (befor + 1 != after) {
return false;
}
for (int k = i; k < i + j; k++) {
if (text.charAt(k) != after) {
return false;
}
}
}
} catch (StringIndexOutOfBoundsException sioobe) {
return false;
}
return true;
}
Wie wärs mit Benchmarktests gegeneinander?
Ich würde sagen, ihm wurde mehr als genug geholfenHallo! Ich versuch die Zeichen in eine String zu vergleichen und zu überprüfen ob der String so aussieht (abbcccdddd....).
Deins sagt falsch für:
String test = "abbeeecccc";
, bei dem der n-te Buchstabe jeweils auch n mal hintereinander auftritt.
Das ist jetzt AuslegungssacheDer 3-te Buchstabe ist c, tritt aber 0-mal auf.![]()
+ 1
: public static void main(String[] args) {
String text = "abbcbcddddeeeeeffffff";
String text2 = text.toUpperCase();
System.out.println(schnaps(text));
System.out.println(schnaps(text2));
}
static boolean schnaps(String text) {
try {
for (int i = 0, j = 1; i < text.length(); i += j, j++) {
System.out.println(text.substring(i, i + j)); // just an output ....
char befor = i > 0 ? text.charAt(i - 1) : (char) (text.charAt(i) - 1);
char after = text.charAt(i);
if (befor == after) {
return false;
}
for (int k = i; k < i + j; k++) {
if (text.charAt(k) != after) {
return false;
}
}
}
} catch (StringIndexOutOfBoundsException sioobe) {
return false;
}
return true;
}