깊은바다거북
개발 공부 기록
깊은바다거북
전체 방문자
오늘
어제
  • 분류 전체보기 (219)
    • JAVA (9)
    • JavaScript (15)
    • 스파르타코딩클럽 (11)
      • [내일배움단] 웹개발 종합반 개발일지 (5)
      • [내일배움캠프] 프로젝트와 트러블 슈팅 (6)
    • SQL | NoSQL (4)
    • CS 등등 (0)
    • TIL | WIL (173)
    • 기타 에러 해결 (3)
    • 내 살 길 궁리 (4)

인기 글

최근 글

최근 댓글

태그

  • 점화식(Recurrence Relation)
  • Backtracking(백트래킹)
  • 01. 미니 프로젝트
  • 코딩테스트 연습문제
  • 최대 힙(Max Heap)
  • 자바스크립트 기초 문법
  • Leetcode
  • Linked List
  • 자잘한 에러 해결
  • Trie
  • TIT (Today I Troubleshot)
  • 자료 구조
  • BFS(너비우선탐색)
  • tree
  • TypeScript
  • 팀 프로젝트
  • 시간 복잡도
  • Til
  • 프로그래머스
  • Inorder Traversal(중위 순회)
  • 혼자 공부하는 자바스크립트
  • Binary Tree(이진 트리)
  • 최소 힙(Min Heap)
  • leetcode-cli
  • POST / GET 요청
  • DFS(깊이우선탐색)
  • BST(이진 탐색 트리)
  • 재귀 함수
  • 트러블 슈팅 Troubleshooting
  • Preorder Traversal(전위 순회)
hELLO · Designed By 정상우.
깊은바다거북

개발 공부 기록

11/14 (화) 대소문자를 모두 가지는 최장 부분 문자열 구하기 TIL
TIL | WIL

11/14 (화) 대소문자를 모두 가지는 최장 부분 문자열 구하기 TIL

2023. 11. 14. 17:44

공부한 것

  • LeetCode #1763. Longest Nice Substring
    Longest Nice Substring - LeetCode
    Can you solve this real interview question? Longest Nice Substring - A string s is nice if, for every letter of the alphabet that s contains, it appears both in uppercase and lowercase. For example, "abABB" is nice because 'A' and 'a' appear, and 'B' and 'b' appear. However, "abA" is not because 'b' appears, but 'B' does not. Given a string s, return the longest substring of s that is nice. If there are multiple, return the substring of the earliest occurrence. If there are none, return an empty string.   Example 1: Input: s = "YazaAay" Output: "aAa" Explanation: "aAa" is a nice string because 'A/a' is the only letter of the alphabet in s, and both 'A' and 'a' appear. "aAa" is the longest nice substring. Example 2: Input: s = "Bb" Output: "Bb" Explanation: "Bb" is a nice string because both 'B' and 'b' appear. The whole string is a substring. Example 3: Input: s = "c" Output: "" Explanation: There are no nice substrings.   Constraints: * 1 <= s.length <= 100 * s consists of uppercase and lowercase English letters.
    https://leetcode.com/problems/longest-nice-substring/description/
    • Divide and Conquer(분할 정복법)를 이해하기에 아주 좋은 다이어그램이 있어 가져와보았다:
      출처: https://leetcode.com/problems/longest-nice-substring/solutions/2237699/javascript-brute-force-and-divide-and-conquer-with-illustrations/
    • 해답 코드:
      // Divide and Conquer 풀이: 
      function longestNiceSubstring2(s: string): string {
      	// 탈출 조건: s의 길이가 0 혹은 1인 경우, 대-소문자 짝이 있을 수 없으므로 (최장 부분 문자열은)빈 문자열 반환.
      	if (s.length < 2)
      		return '';
      
      	let arr = [...s];
      	let set = new Set(arr);
      
      	for (let i = 0; i < arr.length; i++) {
      		const c = arr[i];
      		// 현재 문자 c의 대소문자 버전이 모두 현재 부분 문자열 s에 존재하면: 현재 부분 문자열 s의 다음 문자 검사를 계속한다(=s를 둘로 쪼개지 않는다).  
      		if (set.has(c.toUpperCase()) && set.has(c.toLowerCase()))
      			continue;
      
      		// 현재 문자 c의 대소문자가 현재 부분 문자열 s에 들어있지 않으면: 현재 문자 c를 기준으로 반절을 가른 부분 문자열 각각에서 최장 부분 문자열을 추출한다. s의 길이가 < 2에 도달할 때까지 반복하게 됨.
      		const sub1: string = longestNiceSubstring2(s.substring(0, i));
      		const sub2: string = longestNiceSubstring2(s.substring(i + 1));
      
      		// 두 (최장)부분 문자열 중 길이가 더 긴 쪽을 반환한다.
      		return sub1.length >= sub2.length ? sub1 : sub2;
      	}
      
      	return s;
      }


Uploaded by N2T

    'TIL | WIL' 카테고리의 다른 글
    • 12/5 (화) D3.js로 그래프 자료구조 시각화하기 TIL
    • 11/3 (금) 합이 가장 큰 부분 배열 TIL
    • 11/2 (목) 쿼리를 포함하기 위한 최소 인터벌 TIL
    • 10/28 (토) (완료)인터벌이 안 겹치도록 삭제해야 하는 최소 인터벌 개수 TIL
    깊은바다거북
    깊은바다거북

    티스토리툴바