[Mock] Apple 3

shsh·2021년 4월 12일
0

Mock

목록 보기
28/93

1002. Find Common Characters

Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.

You may return the answer in any order.

My Answer 1: Accepted (Runtime: 40 ms - 92.75% / Memory Usage: 14.4 MB - 31.41%)

class Solution:
    def commonChars(self, A: List[str]) -> List[str]:
        result = []
        for i in range(len(A[0])):
            result.append(A[0][i])
            for j in range(1, len(A)):
                if A[0][i] not in A[j]:
                    result.pop()
                    break
                else:
                    # 한번 찾은 부분은 또 사용하면 안되니까 없애줌
                    tmp = A[j].index(A[0][i])
                    A[j] = A[j][:tmp] + A[j][tmp+1:]
        
        return result

A[0] 을 기준으로 각 문자들이 모든 문자열에 있는지 확인
한번 찾은 부분은 또 사용하면 안되니까 잘라내줌

다른 괜찮은 방식 뭐 떠오르는게 없네요...


1221. Split a String in Balanced Strings

Balanced strings are those that have an equal quantity of 'L' and 'R' characters.

Given a balanced string s, split it in the maximum amount of balanced strings.

Return the maximum amount of split balanced strings.

My Answer 1: Accepted (Runtime: 32 ms - 56.85% / Memory Usage: 13.9 MB - 98.16%)

class Solution:
    def balancedStringSplit(self, s: str) -> int:
        left = 0
        right = 0
        result = 0
        
        for i in range(len(s)):
            if s[i] == "L":
                left += 1
            if s[i] == "R":
                right += 1
                
            if left == right:
                result += 1
        
        return result

L, R 개수 세줘서 같을때마다 result + 1

profile
Hello, World!

0개의 댓글

Powered by GraphCDN, the GraphQL CDN