코딩테스트

844. Backspace String Compare

nayoon 2024. 2. 12. 19:19

https://leetcode.com/problems/backspace-string-compare/description/

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

class Solution {
    public boolean backspaceCompare(String s, String t) {
        List<Character> sList = getList(s);
        List<Character> tList = getList(t);

        return compare(sList, tList);
    }

    public boolean compare(List<Character> sList, List<Character> tList) {
        if (sList.size() != tList.size()) {
            return false;
        }

        for(int i = 0; i < sList.size(); i++) {
            if (sList.get(i) != tList.get(i)) {
                return false;
            }
        }
        return true;
    }

    public List<Character> getList(String s) {
        List<Character> list = new ArrayList<>();

        for(int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '#') {
                if (list.size() == 0) {
                    continue;
                }
                list.remove(list.size() - 1);
                continue;
            }
            list.add(s.charAt(i));
        }

        return list;
    }
}

'코딩테스트' 카테고리의 다른 글

234. Palindrome Linked List  (0) 2024.02.16
14. Longest Common Prefix  (0) 2024.02.14
13. Roman to Integer  (0) 2024.02.08
136. Single Number  (1) 2024.02.07
876. Middle of the Linked List  (0) 2024.02.05