{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 문자열 조작" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 그룹 애너그램" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "문자열 배열을 받아 애너그램 단위로 그룹핑하라." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import collections" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def groupAnagrams(strs):\n", " dic = collections.defaultdict(list)\n", " for s in strs:\n", " dic[''.join(sorted(s))].append(s)\n", " return list(dic.values())" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "ex = ['eat', 'tea', 'tan', 'ate', 'nat', 'bat']" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "groupAnagrams(ex)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\tAccepted\t92 ms\t17.2 MB\tpython3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "파이썬에서는 기본 정렬로 팀소트를 사용한다\n", "\n", "최선일때 오메가(n) 으로 퀵이나 병합보다 좋다" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }