[HackerRank][Python3] Weighted Uniform Strings
2018. 9. 4. 00:32 |
프로그래밍/HackerRank
Problem :
https://www.hackerrank.com/challenges/weighted-uniform-string/problem
My Solution :
#!/usr/bin/env python3
def weighted_uniform_strings(s, queries):
U = set()
for i in range(len(s)):
if i == 0 or s[i] != s[i-1]:
weight = ord(s[i]) - 96
else:
weight += ord(s[i]) - 96
U.add(weight)
for query in queries:
if query in U:
yield 'Yes'
else:
yield 'No'
s = input()
queries_count = int(input())
queries = []
for _ in range(queries_count):
queries_item = int(input())
queries.append(queries_item)
result = weighted_uniform_strings(s, queries)
for ans in result:
print(ans)
Comment :
효율적인 알고리즘을 구현하려면 항상 dictionary(HashMap) 또는 set(HashSet) 자료구조를 고려해야 한다. 탐색 비용이 O(1)이기 때문이다. list(ArrayList)에서 in 또는 count 연산을 반복 수행하는 행위는 최악의 결과를 가져온다.
'프로그래밍 > HackerRank' 카테고리의 다른 글
| [HackerRank][Python3] Sequence Equation (0) | 2018.09.05 |
|---|---|
| [HackerRank][Python3] String Construction (0) | 2018.09.05 |
| [HackerRank][Python3] Beautiful Binary String (0) | 2018.09.05 |
| [HackerRank][Python3] Separate the Numbers (0) | 2018.09.04 |
| [HackerRank][Python3] HackerRank in a String! (0) | 2018.09.03 |
| [HackerRank][Python3] Truck Tour (0) | 2018.09.03 |
| [HackerRank][Python3] Waiter (0) | 2018.09.03 |
| [HackerRank][Python3] Simple Text Editor (0) | 2018.09.02 |
최근에 달린 댓글 최근에 달린 댓글