1-Bit and 2-Bit Characters

The idea to solve this problem is to first, get the total number of bits then iterate through each bit and if the previous bit is 1, then deduct 2 from the total number of bits. If it’s a 0, then deduct only 1. In the end, if the total number of bits is equal to 1, then last character must be a 1-bit character.

class Solution {
    fun isOneBitCharacter(bits: IntArray): Boolean {
        if(bits.size == 0) return false
        if(bits.size == 1) return bits[0] == 0
        if(bits.size == 2) return bits[0] != 1
        
        
        var totalChars = bits.size
        var i = 1
        while(i < bits.size){
            if(bits[i-1] == 1){
                totalChars -= 2
                i++
            }else{
                totalChars -= 1
            }
            
            i++
        }

        return totalChars == 1
    }
}
Runtime: 168 ms, faster than 60.00% of Kotlin online submissions for 1-bit and 2-bit Characters.
Memory Usage: 33.9 MB, less than 100.00% of Kotlin online submissions for 1-bit and 2-bit Characters.