My simple one-pass O(n) solution in Java


  • 0
    A

    The last character must be 0 for the answer to be possibly true. Yet, when we traverse the array from left to right, the encoding length must be 2 if the new traversed element is 1, (either 10 or 11) and 1 if the new traversed element is 0

    class Solution {
        public boolean isOneBitCharacter(int[] bits) {
            int len = bits.length;
            int i=0;
            while(i<len){
                if(i == len-1 && bits[len-1] == 0){
                    return true;
                }
                if(bits[i] == 1){
                    i += 2;
                }else if(bits[i] == 0){
                    i++;
                }
            }
            
            return false;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.