242. Valid Anagram
Easy
5643231Add to ListShareGiven two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
두 개의 문자열 s와 t가 주어지면 t가 s의 아나그램이면 true를 반환하고 그렇지 않으면 false를 반환합니다.
Anagram은 일반적으로 모든 원래 문자를 정확히 한 번 사용하여
다른 단어 또는 구의 문자를 재배열하여 형성된 단어 또는 구입니다.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Constraints:
- 1 <= s.length, t.length <= 5 * 104
- s and t consist of lowercase English letters.
이전에 풀었던 문제와 비슷한 방식으로 풀 수 있었다.
Hash Table을 이용하여 풀었기에 o(n)의 timeComplexity와 o(1)의 spaceComplexity를 가진다.
class Solution {
public:
bool isAnagram(string s, string t) {
unordered_map<char,int> mp;
int sSize = s.size();
int tSize = t.size();
if(sSize != tSize){
return false;
}
for(int i=0; i<sSize; i++) {
mp[s[i]]++; // mp[s[i]] 에 접근 후 int value ++;
mp[t[i]]--; // mp[t[i]] 에 접근 후 int value --;
} // 서로 angram이라면 int값은 0이 됨
for(int j =0; j < sSize; j++){
if(mp[s[j]] != 0){
return false;
}
}
return true;
}
};
https://open.kakao.com/o/gsMhUFie
C/C++/C# 언리얼/유니티 /질문
#C++#C#언리얼#게임개발#질문#개발#자료구조#백준#프로그래머스#c#유니티#unity#enreal
open.kakao.com
'LeetCode' 카테고리의 다른 글
387. First Unique Character in a String 문제풀이 (0) | 2022.07.19 |
---|---|
20. Valid Parentheses 문제풀이 (0) | 2022.07.13 |
415. Add Strings 문제 c++ (0) | 2022.07.10 |
125. Valid Palindrome 문제 (0) | 2022.07.10 |
796. Rotate String 문제 (0) | 2022.07.07 |
댓글