Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion
Common characters of two strings
MoyenMap and Set10 min
Write a function commonCharacters(s1, s2) that finds the letters that occur in both strings: their intersection.
s1ands2are strings. They can be empty and can have spaces, digits and punctuation.- Only the Latin letters
a-zcount. Spaces, digits and other characters are never in the result. Letters with accents, likeé, are not ina-zand do not count either. - Case does not matter (case insensitive):
'J'in one string and'j'in the other is a common letter. - Return an array of letters in lower case. Every letter is in it once, even if it occurs many times.
- Sort the array in alphabetical order.
- If there are no common letters, return an empty array.
A Set of the letters of each string makes this short.
Exemples
commonCharacters('Java With Collections', 'Java Selenium')Renvoie [ 'a', 'e', 'i', 'j', 'l', 'n', 's', 'v' ]
commonCharacters('Hello', 'World')Renvoie [ 'l', 'o' ]
commonCharacters('abc', 'xyz')Renvoie []
Posé chez Synechron · Source
Chargement de l'éditeur...
- Entrée
commonCharacters('Java With Collections', 'Java Selenium')Attendu[ 'a', 'e', 'i', 'j', 'l', 'n', 's', 'v' ] - Entrée
commonCharacters('Hello', 'World')Attendu[ 'l', 'o' ] - Entrée
commonCharacters('abc', 'xyz')Attendu[]
Les tests cachés se lancent à la soumission