In this blog post, I'll share a quick Git tip to help you keep your repositories clean and organized. If you have a Git repository with many branches, it can be useful to remove all branches except the main or master branch. Let's dive into the steps to achieve this.

Open your terminal and navigate to the Git repository you want to clean up.

Run the following command:

`git branch | grep -v "main\|master" | xargs -p git branch -D`

You will see the output below

`git branch -D test test2?...`

Press 'y' and hit enter if you are satisfied with the execution plan. Then, `git branch -D`

will be executed:

`git branch -D test test2?...yDeleted branch test (was ac8a396).Deleted branch test2 (was ac8a396).`

Let's break down the command step by step:

This command lists all branches. Assume you have three branches: `main`

, `test`

, and `test2`

. You will see the output below:

`* main test test2`

`grep`

is a command used to search plain-text data that matches a regular expression. With `-v`

, it excludes the main and master branches from the output.

`xargs`

is a command used to build and execute commands from standard input. In this case, its input comes from `grep -v`

, which is a list of branches excluding `main`

and `master`

. The `-p`

argument allows you to review the execution and decide whether to continue.

By removing unnecessary branches, you can keep your Git repository organized and improve your workflow. Give it a try! Let us know if you have any questions or suggestions in the comments below.

]]>

O(n) time | O(n) space where n is the number of nodes of the binary tree

Runtime: 32 ms, faster than 72.26% of Python3 online submissions for Sum Root to Leaf Numbers.

Memory Usage: 14.2 MB, less than 79.39% of Python3 online submissions for Sum Root to Leaf Numbers.

`def sumNumbers(self, root: Optional[TreeNode]) -> int: # Create a stack to store the nodes we are going to traverse. # For each node, we also store the current number sums up to # the parent node of current node. So we use 0 as the initial # value since current number sumps up to the parenat node of # root is zero. nodeStack = [[root, 0]] res = 0 # When the node stack is not empty, keep popping out the last # item in the stack while nodeStack: # Get the current node and current number from the stack currentNode, currentNum = nodeStack.pop() # Check if this node exist if currentNode: # Add the current node vale into the current number # which sums up from root node to the current node currentNum += currentNode.val # If there's no left and right child node, it means # the current node is a leaf node. # Then we can add the current number to the result. if not currentNode.left and not currentNode.right: res += currentNum # Next, append the left child and right child to the # node stack. When goes to the next level, we need to # time current num by 10 since it represent the number # of next digit nodeStack.append([currentNode.left, currentNum * 10]) nodeStack.append([currentNode.right, currentNum * 10]) # Once traverse all the node in stack, we can return the result return res`

]]>In each step, we need to decide whether to run the Copy All or the Paste operation. To achieve the minimum steps, we should greedily run the Copy All operation as much as we can.

When can we greedily run the Copy All operation? We just need to check whether the remaining length of `'A'`

to be pasted can be divided by the length of the newly copied `'A'`

after the current Paste operation.

`Input: n = 4Output: 4`

Explanation: Initially, we have one character `'A'`

.In step 1, we can only use the Copy All operation. The current length is 1.

In step 2, we can only use the Paste operation. The current length is 2.

In step 3, now we need to decide if using the Copy All operation is okay. If we use the Copy All operation here, the new copied length becomes 2. The remaining length to be pasted is 4 - 2 = 2 which can be dived by the new copied length 2. Hence, we can greedily run the Copy All operation.

In step 4, we use the Paste operation. The current length is 4.

`class Solution: def minSteps(self, n: int) -> int: step, curLen, copyLen = 0, 1, 1 while curLen < n: # The length of the potential copy nextCopyLen = curLen + copyLen curLen += copyLen # Run the Paste operation if (n - nextCopyLen) % nextCopyLen: step += 1 else: # Run the Copy All and the Paste Operation copyLen = nextCopyLen step += 2 return step`

`O(n)`

time in the worst case since we might need to run the Paste operation with copied length 1.

`O(log(n))`

time in the average case since we can double the copied length with each Copy All operation.

`O(1)`

space since we only use some additional variables.

Since we need to calculate the number of positions at which the corresponding bits are different between two integers x and y. It would be really easy to use exclusive or (XOR) to solve this problem since XOR returns 1 only when the input bits are different. You can check the wiki for more information.

Use example 1 in the problem description.

`Input: x = 1, y = 4Output: 2`

`1 (0 0 0 1)4 (0 1 0 0) `

When you do `x ^ y`

which is `1 ^ 4`

, the return value would be `5 (0 1 0 1)`

.

(`^`

is the XOR operator in Python)

`1 (0 0 0 1)^4 (0 1 0 0)------------5 (0 1 0 1)`

Then we only need to count how many 1 appears in the return value of x ^ y. In this example, there are two 1 appears so the answer is 2.

` def hammingDistance(self, x: int, y: int) -> int: # Use bin() to convert an integer number to a binary string xor, differentBit = bin(x ^ y), 0 for bit in xor: if bit == "1": differentBit += 1 return differentBit`

`# O(n) time | O(1) space - where n is the number of bits to represent x^y`

To check whether two strings are anagram, one easy way is to count how many unique characters these two strings have and check if they share the same counts.

For this question, the brute force solution is to build hash maps for every substring with the length of string `p`

from string `s`

and check if they share the same character counts. And it leads to time complexity: `m * n`

where `m`

is the length of string `p`

and `n`

is the length of string `s`

because you need to build up the hash map of character counts when you traverse all the substring with the length of string `p`

from string `s`

.

To improve the time complexity, you can use the sliding window technique to just update the existing hash map of character counts from the last substring of string `s`

instead of building up a new one.

Assume the length of string `p`

is 2, for the first iteration, you build up a hash map for substring from index `0`

to index `1`

if you start from index `0`

. For the second iteration, you don't need to build up a new hash map for substring from index `1`

to index `2`

. You just need to decrease the character count for the character on index 0 and increase the character count for the character on index 1. Then you keep doing this until you reach the end of string `s`

. One thing you need to pay attention to is that you need to remove the key from the hash map if such key's value reaches 0 or the hash map comparison would fail.

`Input: s = "abcb", p = "ab"`

The substring with start index = 0 is `"ab"`

, `sCount = {"a":1, "b":1}`

, same as `pCount = {"a":1, "b":1}`

.

The substring with start index = 1 is `"bc"`

, decrease `sCount["a"]`

by 1 and remove it from the hash map, increase `sCount["c"] by 1`

, `sCount = {"b":1, "c":1}`

.

The substring with start index = 2 is `"ab"`

, decrease `sCount["b"]`

by 1, increase `sCount["b"]`

by 1, `sCount = {"b":1, "c":1}`

.

`class Solution: def findAnagrams(self, s: str, p: str) -> List[int]: if len(p) > len(s): return [] pCount, sCount = {}, {} for i in range(len(p)): pCount[p[i]] = pCount.get(p[i], 0) + 1 sCount[s[i]] = sCount.get(s[i], 0) + 1 result = [0] if pCount == sCount else [] l, r = 0, len(p) while r < len(s): sCount[s[r]] = sCount.get(s[r], 0) + 1 sCount[s[l]] -= 1 if sCount[s[l]] == 0: sCount.pop(s[l]) l, r = l + 1, r + 1 if sCount == pCount: result.append(l) return result`

`O(26n) time | O(52) space`

Since the input strings `s`

and `p`

only consist of lowercase English letters, the maximum number of keys in the hash map is 26. `n`

is the length of the input string `s`

.Each time we compare the two hash maps, we only need to compare it 26 times for the worst case.

In this question, we need to figure out the two numbers which appear only once.The first step is to xor all the numbers in the array, you can get a number that has at least a one-bit difference between the two numbers.Use the number from Example 1:

`Input: nums = [1,2,1,3,2,5]Output: [3,5]Explanation: [5, 3] is also a valid answer.`

The number 3 is 011 and the number 5 is 101 so it has a different bit at the second the 2nd and 3rd bit.Then we can use this to divide all the numbers into two groups.

Let say if we use the rightmost different bit between the number 3 and the number 5.The input array can be divided into two groups as below.

`Group 1: [2, 3, 2] -> these numbers' 2nd bit are 1Group 2: [1, 1, 5] -> these numbers' 2nd bit are 0`

Finally, we can xor all the numbers within these two groups to get the number which appears only once in each group.

`Group 1 after xor is [3]Group 2 after xor is [5]`

`def singleNumber(self, nums: List[int]) -> List[int]: xor = 0 for num in nums: # xor all the number in the input array xor ^= num rightmost1Bit = 1 # Find the rightmost different bit which returns 1 with & operation while xor & rightmost1Bit == 0: rightmost1Bit <<= 1 res = [0, 0] # Divide the number in the input array into two groups and do xor at the same time. for num in nums: # Use & operator to find the number with the same rightmost 1 bit. if num & rightmost1Bit: res[0] ^= num else: res[1] ^= num return res`

`O(n) time | O(1) space - where n is the length of the input array`

The criteria to be a judge can be translated into something like

- The number of people who trust judge can only be
`n - 1`

- Judge can only trust
`0`

people

It means if we only need to count if there is a person who has exactly `n - 1`

trust from the others. But how do we know if the person does not trust anyone? It can be achieved by decreasing the count of trusts by `1`

if the person trusts anyone.

Let's take a look at example 3 in the description:

`n = 3, trust = [[1,3],[2,3],[3,1]]`

We can initialize the count array as `[0] * (n + 1)`

. (Index 0 is not going to be used since the people label starts from 1. Then we iterate the `trust`

array. )

`[0, 0, 0, 0]`

The first subarray is `[1, 3]`

so we need to set `count[1] = count[1] - 1`

because people `1`

trusts someone. And we need to set `count[3] = count[3] + 1`

because people 3 got one trust.

`[0, -1, 0, 1]`

The second subarray is `[2, 3]`

so the `count`

array becomes

`[0, -1, -1, 2]`

The third subarray is `[3, 1]`

. You may notice that the person with label `3`

trusts the person with label `1`

which makes the people with label `3`

does not the criteria to be a judge. That is why we need to set the `count[3] = count[3] - 1`

so the `count[3]`

is not equal to `n - 1`

after we iterating the `trust`

array. Count array becomes:

`[0, 0, -1, 1]`

Hence, nobody meets the criteria to be a judge because nobody's count is equal to

`n - 1 = 2`

`def findJudge(self, n: int, trust: List[List[int]]) -> int: count = [0] * (n + 1) # Count the trust for a, b in trust: count[a] -= 1 count[b] += 1 # Check if there is a person who has exactly n - 1 trust for i in range(1, n + 1): if count[i] == n - 1: return i return -1`

`O(n) time | O(n) space - where n is the number of people`

We need to iterate the trust array to count the trust so the time complexity is O(n). We need an additional array to store the trust count so the space complexity is O(n) too.

]]>