Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions valid-anagram/jjipper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* https://leetcode.com/problems/valid-anagram/
* time complexity : O(n)
* space complexity : O(n)
*/

function isAnagram(s: string, t: string): boolean {
const count: Record<string, number> = {};
if (s.length !== t.length) return false;

for (let char of s) {
if (count[char] === undefined) {
count[char] = 1;
} else {
count[char] += 1;
}
}

for (let char of t) {
if (!count[char]) {
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 early return하는 것도 좋은 방법이네요! 하나 얻어갑니다 👍🏼

} else {
count[char] -= 1;
}
}

return Object.values(count).every(v => v === 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ts는 처음 보는데 전체 요소 체크를 every로 하는 게 인상 깊네요!

};
Loading