2309. The best English letters with both appropriates and lowercases
Thought:
- Hash table:The first thing to think of the question is to solve it with a dictionary。But find that you can directly traverse the alphabet,fromZStart looking for,See if the applause exists,Just return directly。
Running time exceeds99.9%
2. Bit operation:We can use two integers mask1 and mask2 Record string separately s 中出现的小写字母and大写字母,in mask1 First i
Position representation i Does a lowercase letter appear,and mask2 First i Position representation i Whether an uppercase letter appears。
Then we will mask1 and mask2 Perform and calculate,The results obtained mask First i Position representation i Whether the lethals of the letter appear at the same time。
As long as you get mask The highest level of binary representation 1 s position,Convert it to the corresponding capital letter。If all binary positions are not 1,Explain that there is no letter that appears at the same time,,Return to an empty string。
author:ylb
Link:https://leetcode.cn/problems/greatest-english-letter-in-upper-and-lower-case/solutions/2077636/by-lcbin-zbg0/
source:Deduction(LeetCode)
著作权归author所有。商业转载请联系author获得授权,Non -commercial reprint Please indicate the source。
Code:
1 | class Solution: |
1 | class Solution { |
1 | class Solution: |