Home > leetcode > First Unique Character in a String (Kotlin)

First Unique Character in a String (Kotlin)


Today we solve a very popular 'kata' on Leetcode #387 First Unique Character in a String

Difficulty level: Easy

Related Topics: Hash Table, String

Disclaimer: You might have different results in Runtime and Memory Usage, it's okay 🤣 it how it works.


Description:

Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode"
return 2.

Note:

 You may assume the string contains only lowercase English letters.

And your playground looks like this:

class Solution {
    fun firstUniqChar(s: String): Int {

    }
};

Solution 1

At first, let's solve this by using a hash table aka HashMap.

The idea is to go thru the string and store how many times a letter appears in the string.

val hashTable = HashMap<Char, Int>()

Let's init hashtable where the key has the type of Char and value has the type of Int.

for (char: Char in s) {
    if (hashTable.containsKey(char)) {
        hashTable[char] = hashTable[char]!! + 1
    } else {
        hashTable[char] = 1
    }
}

And our HashMap looks like this:

c=1, t=1, d=1, e=3, l=1, o=1

Now we have to return the first unique character, but as you can see object doesn't guarantee order. That's why we have to loop over the string and find the first letter with value 1. Or return -1 if there is no unique character.

 s.forEachIndexed { index, char ->
    if (hashTable[char] == 1) {
        return index
    }
}

return -1

We go thru string letter by letter and check where this letter has value 1 in letterCounter if so, we return its number.

Full solution

class Solution {
    fun firstUniqChar(s: String): Int {

    val hashTable = HashMap<Char, Int>()

     for (char: Char in s) {

        if (hashTable.containsKey(char)) {
            hashTable[char] = hashTable[char]!! + 1
        } else {
            hashTable[char] = 1
        }
     }


     s.forEachIndexed { index, char ->
        if (hashTable[char] == 1) {
            return index
        }
    }

    return -1
    }
}

Let's submit it and see the result.

Runtime: 284 ms, faster than 69.17% of Kotlin online submissions for First Unique Character in a String.
Memory Usage: 34.8 MB, less than 80.00% of Kotlin online submissions for First Unique Character in a String.

Solution 2

Let's try a different approach.

If indexOf() equal lastIndexOf() this is a unique character. So we have to loop over the string and return first unique character.

class Solution {
    fun firstUniqChar(s: String): Int {

        s.forEachIndexed { index, char ->
            if(s.indexOf(char) == s.lastIndexOf(char)) {
                return index
            }
        }

        return -1
     }
}

Let's submit it and see the result.

Runtime: 236 ms, faster than 86.47% of Kotlin online submissions for First Unique Character in a String.
Memory Usage: 35.2 MB, less than 20.00% of Kotlin online submissions for First Unique Character in a String.
© 2021, Andrew Losseff