Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

10 of 10
Commonmark migration

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]

Try it here.

-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 true if it complies to any of them (and false otherwise):
    1. If the five characters are distributed like: 1<2<3>4>5 (i.e. ALPHA)
    2. If the five characters are distributed like: 1>2<3<4>5 (i.e. EAGLE, HARSH, NINON, PINUP)
    3. If the five characters are distributed like: 1<2>3>4<5 (i.e. RULER)
    4. If the five characters are distributed like: 1>2>3<4<5 (i.e. THEFT, WIDOW)

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

AltStyle によって変換されたページ (->オリジナル) /