[코테 풀이] Minimize String Length

시내·2024년 6월 29일
0

Q_2716) Minimize String Length

출처 : https://leetcode.com/problems/minimize-string-length/

Given a string s, you have two types of operation:

  • Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the left of i (if exists).

  • Choose an index i in the string, and let c be the character in position i. Delete the closest occurrence of c to the right of i (if exists).

Your task is to minimize the length of s by performing the above operations zero or more times.

Return an integer denoting the length of the minimized string.

class Solution {
    public int minimizedStringLength(String s) {
         Set<Character> set= new HashSet<>();
        for(int i = 0; i < s.length(); i++){
            if(!set.contains(s.charAt(i))) set.add(s.charAt(i));
        }
        return set.size();
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글