Oracle sql with regexp based on hackerrank problem

93 Views Asked by At

i do this

select distinct city 
from station 
where regexp_like(city,'^[^aeiou].*[^aeiou]$','i') and regexp_like(city,'^[^aeiou]\w+[^aeiou]$','i');

. it is wrong i know. but can someone explain to me where i am wrong at based on the question ? any answer will appreciated. thank you.

here link screenshot from the question.

hackerrank problem

1

There are 1 best solutions below

0
Littlefoot On

Does it have to be regex? substr handles it in a simple manner:

SQL> with station (city) as
  2    (select 'Zagreb' from dual union all
  3     select 'Athens' from dual union all
  4     select 'Rome'   from dual union all
  5     select 'Ottawa' from dual
  6    )
  7  select distinct city
  8      from station
  9      where upper(substr(city, 1, 1)) not in ('A', 'E', 'I', 'O', 'U')
 10        and upper(substr(city, -1))   not in ('A', 'E', 'I', 'O', 'U');

CITY
------
Zagreb

SQL>

If you must use regex, then

  7  select distinct city
  8      from station
  9      where regexp_like(city, '^[^aeiou].*[^aeiou]$', 'i');

CITY
------
Zagreb

SQL>