반응형
https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters
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:
int myFlag = 0;
int maxLength(vector<string>& arr) {
return maxLength(arr, 0);
}
int maxLength(vector<string>& arr, int idx) {
if(arr.size() == idx) {
return count(myFlag);
}
int res = maxLength(arr, idx + 1);
if(canConcatenate(myFlag, arr[idx])) {
check(myFlag, arr[idx]);
res = max(res, maxLength(arr, idx + 1));
uncheck(myFlag, arr[idx]);
}
return res;
}
private:
bool canConcatenate(int myFlag, string& str) {
return !isChecked(myFlag, str) && !hasDuplicated(str);
}
int count(int flag) {
int c = 0;
for(int i=0; i<26; i++) {
if((flag >> i) & 1) c++;
}
return c;
}
void check(int& flag, string& str) {
for(char c : str) check(flag, c);
}
void check(int& flag, char c) {
flag |= (1 << (c - 'a'));
}
void uncheck(int& flag, string& str) {
for(char c : str) uncheck(flag, c);
}
void uncheck(int& flag, char c) {
flag ^= (1 << (c - 'a'));
}
bool isChecked(int flag, string& str) {
for(char c : str)
if(isChecked(flag, c)) return true;
return false;
}
bool isChecked(int flag, char c) {
return (flag >> (c - 'a')) & 1;
}
bool hasDuplicated(string& str) {
int flag = 0;
for(char c : str) {
if(isChecked(flag, c)) return true;
check(flag, c);
}
return false;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 451. Sort Characters By Frequency (0) | 2024.02.07 |
---|---|
LeetCode 1457. Pseudo-Palindromic Paths in a Binary Tree (0) | 2024.01.25 |
LeetCode 645. Set Mismatch (1) | 2024.01.22 |
LeetCode 931. Minimum Falling Path Sum (0) | 2024.01.20 |
LeetCode 70. Climbing Stairs (0) | 2024.01.18 |