Description
A Bitset is a data structure that compactly stores bits.
Implement the Bitset class:
Bitset(int size)Initializes the Bitset withsizebits, all of which are0.void fix(int idx)Updates the value of the bit at the indexidxto1. If the value was already1, no change occurs.void unfix(int idx)Updates the value of the bit at the indexidxto0. If the value was already0, no change occurs.void flip()Flips the values of each bit in the Bitset. In other words, all bits with value0will now have value1and vice versa.boolean all()Checks if the value of each bit in the Bitset is1. Returnstrueif it satisfies the condition,falseotherwise.boolean one()Checks if there is at least one bit in the Bitset with value1. Returnstrueif it satisfies the condition,falseotherwise.int count()Returns the total number of bits in the Bitset which have value1.String toString()Returns the current composition of the Bitset. Note that in the resultant string, the character at theithindex should coincide with the value at theithbit of the Bitset.
Β
Example 1:
Input ["Bitset", "fix", "fix", "flip", "all", "unfix", "flip", "one", "unfix", "count", "toString"] [[5], [3], [1], [], [], [0], [], [], [0], [], []] Output [null, null, null, null, false, null, null, true, null, 2, "01010"] Explanation Bitset bs = new Bitset(5); // bitset = "00000". bs.fix(3); // the value at idx = 3 is updated to 1, so bitset = "00010". bs.fix(1); // the value at idx = 1 is updated to 1, so bitset = "01010". bs.flip(); // the value of each bit is flipped, so bitset = "10101". bs.all(); // return False, as not all values of the bitset are 1. bs.unfix(0); // the value at idx = 0 is updated to 0, so bitset = "00101". bs.flip(); // the value of each bit is flipped, so bitset = "11010". bs.one(); // return True, as there is at least 1 index with value 1. bs.unfix(0); // the value at idx = 0 is updated to 0, so bitset = "01010". bs.count(); // return 2, as there are 2 bits with value 1. bs.toString(); // return "01010", which is the composition of bitset.
Β
Constraints:
1 <= size <= 1050 <= idx <= size - 1- At most 
105calls will be made in total tofix,unfix,flip,all,one,count, andtoString. - At least one call will be made to 
all,one,count, ortoString. - At most 
5calls will be made totoString. 
Solution
Python3
class Bitset:
 
    def __init__(self, size: int):
        self.n = size
        self.mask = [0] * size
        self.counts = 0
        self.hasFlip = False
 
    def fix(self, idx: int) -> None:
        if not self.hasFlip:
            self.fixHelper(idx)
        else:
            self.unfixHelper(idx)
        # print(self.toString())
    
    def fixHelper(self, idx):
        n = self.n - idx - 1
        
        if self.mask[idx] == 0:
            self.mask[idx] = 1
            self.counts += 1
 
    def unfix(self, idx: int) -> None:
        if self.hasFlip:
            self.fixHelper(idx)
        else:
            self.unfixHelper(idx)
    
    def unfixHelper(self, idx: int) -> None:
        n = self.n - idx - 1
        
        if self.mask[idx] == 1:
            self.mask[idx] = 0
            self.counts -= 1
        
    def flip(self) -> None:
        self.hasFlip = not self.hasFlip
        
    def all(self) -> bool:
        if not self.hasFlip:
            return self.counts == self.n
        else:
            return self.counts == 0
 
    def one(self) -> bool:
        if not self.hasFlip:
            return self.counts > 0
        else:
            return self.n - self.counts > 0
 
    def count(self) -> int:
        if not self.hasFlip:
            return self.counts
        else:
            return self.n - self.counts
 
    def toString(self) -> str:
        res = []
        
        for x in self.mask:
            if self.hasFlip:
                res.append("1" if x == 0 else "0")
            else:
                res.append(str(x))
        
        return "".join(res)
 
 
# Your Bitset object will be instantiated and called as such:
# obj = Bitset(size)
# obj.fix(idx)
# obj.unfix(idx)
# obj.flip()
# param_4 = obj.all()
# param_5 = obj.one()
# param_6 = obj.count()
# param_7 = obj.toString()