-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jangwonyoon] WEEK 8 Solutions #1901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+96
−0
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* @param {string} s | ||
* @return {number} | ||
* | ||
* 풀이 방법 1 | ||
* | ||
* 1. brute force 를 사용해서 모든 경우의 수를 구한다. | ||
* 2. 투포인터를 통해 isPalindrome 을 확인한다. | ||
* | ||
* 복잡성 | ||
* | ||
* Time Complexity: O(n^2) | ||
* Space Complexity: O(1) | ||
*/ | ||
|
||
/** | ||
* isPalindrome 함수 | ||
*/ | ||
function isPalindrome(s) { | ||
let left = 0; | ||
let right = s.length - 1; | ||
|
||
while (left < right) { | ||
if (s[left] !== s[right]) return false; | ||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
var countSubstrings = function(s) { | ||
let count = 0; | ||
|
||
// 모든 경우의 수를 구한다. | ||
for (let start = 0; start < s.length; start++) { | ||
for (let end = start; end < s.length; end++) { | ||
const subStr = s.slice(start, end + 1); | ||
|
||
// isPalindrome 함수를 통해 팰린드롬인지 확인한다. | ||
if (isPalindrome(subStr)) { | ||
count++; | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
}; | ||
|
||
/** | ||
* 풀이 방법 2 | ||
* | ||
* 1. dfs를 통해 모든 경우의 수를 구한다. | ||
* 2. isPalindrome 함수를 통해 팰린드롬인지 확인한다. | ||
* | ||
* 복잡성 | ||
* | ||
* Time Complexity: O(n^2) | ||
* Space Complexity: O(1) | ||
*/ | ||
|
||
function isPalindrome(s) { | ||
let left = 0; | ||
let right = s.length - 1; | ||
|
||
while (left < right) { | ||
if (s[left] !== s[right]) return false; | ||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
var countSubstrings = function(s) { | ||
let count = 0; | ||
|
||
function dfs(startIdx) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DFS 잘 활용하신 것 같아요 주석설명도 너무 좋습니다 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 감사합니다 :) |
||
// 모든 시작점 탐색 완료 | ||
if (startIdx === s.length) return; | ||
|
||
// 현재 시작점에서 가능한 모든 끝점 확인 | ||
for (let end = startIdx; end < s.length; end++) { | ||
const sub = s.slice(startIdx, end + 1); | ||
if (isPalindrome(sub)) { | ||
count++; | ||
} | ||
} | ||
|
||
// 다음 시작점으로 이동 | ||
dfs(startIdx + 1); | ||
} | ||
|
||
dfs(0); | ||
return count; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안녕하세요. 서브메서드를 만들어서 대칭인지 확인하는 방법 좋습니다!!