일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- C#
- git
- property
- vue.js
- 앙상블
- webpack
- type
- var
- dotenv
- bash
- nginx
- Clone
- Python
- vuetify
- condition
- 보안
- leetcode
- BOJ
- security
- TypeScript
- C++
- JavaScript
- VUE
- AI
- docker
- generic
- loop
- scss
- machine learning
- npm
Archives
- Today
- Total
ice rabbit programming
[LeetCode] 242. Valid Anagram 본문
728x90
https://leetcode.com/problems/valid-anagram/
Valid Anagram - LeetCode
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
간만에 문제풀이를 하여 포스팅하게 되었다.
문제는 간단한 편이다. Anagram 문자열인지를 확인하는 문제로, 두 문자열이 주어지고, 순서와는 관계 없이 사용된 문자가 일치하면 된다.
ex) "anagram"과 "aganram"
여러 방법이 있을 것 같으나, 본인은 Map을 이용하여 사용된 문자 개수를 증감시키고, 검사할 때는 없거나 0이 아니면 false로 판단하도록 하였다.
요즘 다시 C++을 하게 된 관계로.. 오랜만에 C++로 문제를 풀었다.
class Solution {
public:
bool isAnagram(string s, string t) {
bool result = true;
map<char, int> charSet;
for(int i=0; i<s.size(); ++i) {
if(charSet.find(s[i]) != charSet.end()) {
++charSet.find(s[i])->second;
} else {
charSet.insert({s[i], 1});
}
}
for(int i=0; i<t.size(); ++i) {
if(charSet.find(t[i]) != charSet.end()) {
--charSet.find(t[i])->second;
} else {
result = false;
break;
}
}
for (auto iter = charSet.begin() ; iter != charSet.end(); iter++) {
if(iter->second > 0) {
result = false;
break;
}
}
return result;
}
};
728x90
'PS > LeetCode' 카테고리의 다른 글
[LeetCode] 232. Implement Queue Using Stacks (0) | 2022.11.10 |
---|---|
[LeetCode] 766. toeplitz-matrix (0) | 2022.11.10 |
[LeetCode] 231. Power of Two (0) | 2020.12.08 |
[LeetCode] 217. Contains Duplocate (0) | 2020.12.08 |
[LeetCode] 191. Numbers of 1 Bits (0) | 2020.12.08 |