Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'.
If you add periods '.' between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names.
If you add a plus '+' in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered. Note that this rule does not apply to domain names.
Given an array of strings emails where we send one email to each email[i], return the number of different addresses that actually receive mails.
class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
for i in range(len(emails)):
tmp = emails[i].split('@')
tmp[0] = tmp[0].replace(".", "")
if "+" in tmp[0]:
idx = tmp[0].index("+")
tmp[0] = tmp[0][:idx]
emails[i] = '@'.join(tmp)
return len(set(emails))
@
를 기준으로 split 한 다음, 앞부분은 .
과 +
에 대한 처리를 해주고 다시 @
로 묶어줌
중복 제거하기 위해서 set 형으로 바꾼 후 그때의 길이 return
In a row of trees, the i-th tree produces fruit with type tree[i].
You start at any tree of your choice, then repeatedly perform the following steps:
Add one piece of fruit from this tree to your baskets. If you cannot, stop.
Move to the next tree to the right of the current tree. If there is no tree to the right, stop.
Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop.
You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each.
What is the total amount of fruit you can collect with this procedure?
class Solution:
def totalFruit(self, tree: List[int]) -> int:
a = -1
b = -1
result = 0
count = collections.Counter(tree)
if len(count) < 3:
return len(tree)
for i in range(len(tree)):
a = tree[i]
b = -1
cnt = 0
for j in range(i, len(tree)):
if b == -1 and tree[i] != tree[j]:
b = tree[j]
elif a != tree[j] and b != tree[j]:
result = max(result, cnt)
break
cnt += 1
if j == len(tree)-1:
result = max(result, cnt)
return result
a
는 tree[i]
로, b
는 a
와 다른 첫번째 값으로 해서 count
a
, b
와 아예 다른 문자가 나오면 max 값 update 해주고 break
class Solution:
def totalFruit(self, tree: List[int]) -> int:
result = 0
a = tree[0]
b = -1
acnt = 1
bcnt = 0
for i in range(1, len(tree)):
if a == tree[i]:
acnt += 1
elif b == -1:
b = tree[i]
bcnt += 1
elif b == tree[i]:
bcnt += 1
else:
result = max(result, acnt + bcnt)
if tree[i-1] == b:
a = b
acnt = bcnt
b = tree[i]
bcnt = 1
result = max(result, acnt + bcnt)
return result
그냥 반복문 하나로 해보려 했는데 시간이 부족해서 마무리를 못함~~
a
, b
잡아서 각각 count 해주기
a
, b
와 다른 문자가 나오면 max 값 update 하고 a
, b
도 직전의 문자와 새문자로 바꿔줌