Java 8, (削除) 231 (削除ここまで) (削除) 193 (削除ここまで) (削除) 185 (削除ここまで) (削除) 122 (削除ここまで) (削除) 103 (削除ここまで) 78 bytes
s->s.length==5&&(s[1]-s[0])*(s[3]-s[2])<0&(s[2]-s[1])*(s[4]-s[3])<0&s[4]==s[0]
-38 bytes thanks to @dpa97 for reminding me to use char[] instead of String.
-63 bytes thanks to @KarlNapf's derived formula.
-25 bytes by converting it from Java 7 to Java 8 (and now returning a boolean instead of integer).
193 bytes answer:
int c(char[]s){if(s.length!=5)return 0;int a=s[0],b=s[1],c=s[2],d=s[3],e=s[4],z=b-a,y=c-b,x=d-c,w=e-d;return e!=a?0:(z>0&y>0&x<0&w<0)|(z<0&y>0&x>0&w<0)|(z>0&y<0&x<0&w>0)|(z<0&y<0&x>0&w>0)?1:0;}
Explanation:
- If the length of the string isn't 5, we return
false - If the first character doesn't equal the last character, we return
false - Then we check the four valid cases one by one (let's indicate the five characters as 1 through 5), and return
trueif it complies to any of them (andfalseotherwise):- If the five characters are distributed like:
1<2<3>4>5(i.e.ALPHA) - If the five characters are distributed like:
1>2<3<4>5(i.e.EAGLE,HARSH,NINON,PINUP) - If the five characters are distributed like:
1<2>3>4<5(i.e.RULER) - If the five characters are distributed like:
1>2>3<4<5(i.e.THEFT,WIDOW)
- If the five characters are distributed like:
These four rules can be simplified to 1*3<0 and 2*4<0 (thanks to @KarlNapf's Python 2 answer).
Kevin Cruijssen
- 136.2k
- 14
- 154
- 394