반응형
    
    
    
  https://leetcode.com/problems/string-matching-in-an-array/description/
set 과 문자열 검색 함수 find를 사용해본 문제였습니다.
📕 풀이방법
📔 입력 및 초기화
set s와 vector ans를 선언해줍니다.
📔 풀이과정
1. 자기 자신이 아닌 문자열을 서로 비교해 부분 문자열이라면 해당 값을 s에 insert해줍니다.
2. s의 원소들을 ans에 push_back해줍니다.
📔 정답 출력 | 반환
ans를 반환합니다.
📕 Code
📔 C++
class Solution {
public:
    vector<string> stringMatching(vector<string>& words) {
        set <string> s;
        vector <string> ans;
        for(int i = 0; i < words.size(); i++) {
            for(int j = 0; j < words.size(); j++) {
                if(i == j) continue;
                if(words[i].find(words[j]) != string::npos) {
                    s.insert(words[j]);
                }
            }
        }
        
        for(auto c : s) {
            ans.push_back(c);
        }
        
        return ans;
    }
};*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.
'Algorithm > 자료구조' 카테고리의 다른 글
| (C++) - LeetCode (easy) 1528. Shuffle String (0) | 2024.04.21 | 
|---|---|
| (C++) - LeetCode (easy) 1507. Reformat Date (0) | 2024.04.09 | 
| (C++) - LeetCode (easy) 1389. Create Target Array in the Given Order (0) | 2024.03.05 | 
| (C++) - LeetCode (easy) 1331. Rank Transform of an Array (1) | 2024.02.05 | 
| (C++) - LeetCode (easy) 1299. Replace Elements with Greatest Element on Right Side (0) | 2024.01.22 |