Problem overview
The function fileExtension receives a file name and returns its extension: the part after the last dot. A name can contain several dots, and only the text after the final one counts. The extension keeps its letters exactly as they are.
For "photo.jpeg" the answer is "jpeg". For "archive.tar.gz" it is "gz". For "README.MD" it is "MD", with the capital letters kept. The lesson guarantees at least one dot, and no dot at the start or the end of the name.
Approach
Cut the name at every dot and take the last piece. split(".") turns a string into a list of strings, one for each part between the separators. Whatever the number of dots, the extension is the last item of that list.
Step by step
- Split the name with
fileName.split(".")and keep the list in avalnamedparts. Every dot is a cut, so"archive.tar.gz"gives three parts. - Read the last item. A list is indexed like a string, with square brackets, and its length is
parts.size. The last index isparts.size - 1.
The solution in Kotlin
class Solution {
fun fileExtension(fileName: String): String {
val parts = fileName.split(".")
return parts[parts.size - 1]
}
}
Details worth knowing:
- The result is a
List<String>. Printing"archive.tar.gz".split(".")shows[archive, tar, gz]. The list is read-only, which is all this function needs. size, notlength. Strings havelength; lists havesize. Mixing them up is a compile error.- The dot is plain text here. In Kotlin,
split(".")with a string argument splits on that exact text. It isn't a pattern where a dot means "any character", so no escaping is needed. (Only theRegexoverload treats it as a pattern.) - Shortcuts.
fileName.substringAfterLast(".")returns the text after the last dot in one call, andparts.last()returns the last item of a list. For"archive.tar.gz"both give"gz".
Tracing the examples
"photo.jpeg".split(".") gives two parts, photo and jpeg, so parts.size is 2 and the last index is 1: the answer is "jpeg".
"archive.tar.gz" gives [archive, tar, gz]. The size is 3, the last index is 2, and the answer is "gz". The name has two dots, but the code doesn't care how many there are.
"README.MD" gives [README, MD], and the answer is "MD". split copies the text as it is, so no case changes.
Edge cases and common mistakes
- Taking
parts[1]. That works for"photo.jpeg"but returns"tar"for"archive.tar.gz". The last item is the one to read. - Using
parts.sizeas the index. The valid indexes end atparts.size - 1. Indexparts.sizethrows anIndexOutOfBoundsException(anArrayIndexOutOfBoundsException, a kind of it, on arrays). - Names without a dot. The lesson rules them out. For
"noDot"the split gives a list with one item,[noDot], so the function would return the whole name. - Empty parts. Two dots in a row, as in
"a..b", produce an empty string between them:[a, , b]. A dot at the end would end the list with an empty string. The lesson excludes names that end with a dot, which keeps the last part non-empty.
Testing
"photo.jpeg"gives"jpeg"."archive.tar.gz"gives"gz"."README.MD"gives"MD".
