[코테 풀이] Check if All A's Appears Before All B's

시내·2024년 7월 29일
0

Q_2124) Check if All A's Appears Before All B's

출처 : https://leetcode.com/problems/check-if-all-as-appears-before-all-bs/

Given a string s consisting of only the characters 'a' and 'b', return true if every 'a' appears before every 'b' in the string. Otherwise, return false.

class Solution {
    public boolean checkString(String s) {
        List<Integer> arrA = new ArrayList<>();
        List<Integer> arrB = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'a') {
                arrA.add(i);
            } else {
                arrB.add(i);
            }
        }
        if (arrA.size() == 0 || arrB.size() == 0) return true;
        else if (arrA.get(arrA.size() - 1) > arrB.get(0)) {
            return false;
        }
        return true;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글