saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • two pointer
  • 취준
  • Python
  • BFS
  • 딥러닝
  • 개발자
  • 릿코드
  • 알고리즘문제해결
  • LeetCode
  • 문제해결능력
  • 백준
  • 온라인저지
  • 취업준비
  • c++
  • 딕셔너리
  • 알고리즘
  • 리트코드
  • 파이썬
  • DFS
  • 개발자 취업준비

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

컴퓨터공학/LeetCode 1000

[LeetCode - Biweekly Contest 90] 2451. Odd String Difference

2022. 11. 1. 06:13

2451. Odd String Difference

Easy

You are given an array of equal-length strings words. Assume that the length of each string is n.

Each string words[i] can be converted into a difference integer array difference[i] of length n - 1 where difference[i][j] = words[i][j+1] - words[i][j] where 0 <= j <= n - 2. Note that the difference between two letters is the difference between their positions in the alphabet i.e. the position of 'a' is 0, 'b' is 1, and 'z' is 25.

  • For example, for the string "acb", the difference integer array is [2 - 0, 1 - 2] = [2, -1].

All the strings in words have the same difference integer array, except one. You should find that string.

Return the string in words that has different difference integer array.

 

Example 1:

Input: words = ["adc","wzy","abc"]
Output: "abc"
Explanation: 
- The difference integer array of "adc" is [3 - 0, 2 - 3] = [3, -1].
- The difference integer array of "wzy" is [25 - 22, 24 - 25]= [3, -1].
- The difference integer array of "abc" is [1 - 0, 2 - 1] = [1, 1]. 
The odd array out is [1, 1], so we return the corresponding string, "abc".

Example 2:

Input: words = ["aaa","bob","ccc","ddd"]
Output: "bob"
Explanation: All the integer arrays are [0, 0] except for "bob", which corresponds to [13, -13].

 

Constraints:

  • 3 <= words.length <= 100
  • n == words[i].length
  • 2 <= n <= 20
  • words[i] consists of lowercase English letters.

문제 풀이

  • 주어진 단어들 중에 단어의 알파벳 값을 순서대로 계산했을때의 차이 값이 단어 하나만 제외하고 모두 같다. 
  • 차이 값을 계산하는 방법은 뒤에있는 알파벳의 아스키코드 값에 현재 위치의 알파벳의 아스키코드를 빼는 것이다. 
  • 예를 들어 'adc'가 있으면 아스키코드 값은 032인데 이 것을 차이 값으로 만들면 [3 - 0, 2 - 3]이 된다. 
  • 제한 사항을 보면 단어의 총 개수가 최악의 경우 100개이기 때문에 시간 복잡도 상관없이 풀어도된다.
  • dictionary를 두개를 만들고, 각 단어들의 차이 값 배열에 대한 개수를 첫번째 딕셔너리에서 세어준다.
  • 그리고  두번째 딕셔너리에 차이 값 배열을 키로 넣고, 벨류 값으로 단어를 넣는다.
  • 그리고 마지막에 벨류값이 1인 단어를 반환한다.

소스 코드

class Solution:
    def oddString(self, words: List[str]) -> str:
        d = defaultdict(int)
        d2 = dict()
        for i in range(0, len(words)):
            cur = []
            for j in range(1, len(words[i])):
                cur.append((ord(words[i][j]) - ord('a')) - (ord(words[i][j - 1]) - ord('a')))
            cur = tuple(cur)
            d[cur] += 1
            d2[cur] = words[i]
        for k, v in d.items():
            if v == 1:
                return d2[k]
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode - Biweekly Contest 90]2454. Next Greater Element IV  (0) 2022.11.01
[LeetCode - Biweekly Contest 90] 2453. Destroy Sequential Targets  (0) 2022.11.01
[LeetCode - Biweekly Contest 90] 2452. Words Within Two Edits of Dictionary  (1) 2022.11.01
[LeetCode] 49. Group Anagrams  (0) 2022.10.29
[LeetCode] 22. Generate Parentheses  (0) 2022.10.26
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode - Biweekly Contest 90]2454. Next Greater Element IV
    • [LeetCode - Biweekly Contest 90] 2453. Destroy Sequential Targets
    • [LeetCode - Biweekly Contest 90] 2452. Words Within Two Edits of Dictionary
    • [LeetCode] 49. Group Anagrams
    saurus2
    saurus2
    Simple is Best

    티스토리툴바