똑똑한 개발/Algorithm 과 Data Structure
HashMap 기본 (FindFirstUniqueCharacter...)
성댕쓰
2022. 1. 4. 22:52
문제: 주어진 String s에서 반복되지 않는 첫번째 character의 index를 return하여라

def FindFirstUniqueCharacter(stringParam : str) -> int:
hashMap = {}
for c in stringParam:
if c in hashMap:
hashMap[c] += 1
else:
hashMap[c] = 1
for idx, c in enumerate(stringParam):
if hashMap[c] == 1:
return idx
return -1
print(FindFirstUniqueCharacter("nocodeprogram"))
print(FindFirstUniqueCharacter("nownocodeprogram"))
******************
0
2
문제: 주어진 두 string s와 t가 isomorphic 관계인지 찾아서 return하여라

def IsIsomorphic(strA : str, strB : str) -> bool:
hashMap = {}
if len(strA) != len(strB):
return False
for idx in range(0,len(strA)):
if not strA[idx] in hashMap:
hashMap[strA[idx]] = strB[idx]
else:
if strB[idx] == hashMap[strA[idx]]:
continue
else:
return False
return True
print(IsIsomorphic(strA="aaaccd", strB="xxxyyz"))
print(IsIsomorphic(strA="aaaccd", strB="xcxyyz"))
****************
True
False
참조 : 코딩 테스트, 초급, 문제풀이1,2 - YouTube