{"id":176,"date":"2018-05-23T21:15:09","date_gmt":"2018-05-23T13:15:09","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=176"},"modified":"2018-11-27T17:15:46","modified_gmt":"2018-11-27T09:15:46","slug":"387-first-unique-character-in-a-string","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=176","title":{"rendered":"387. First Unique Character in a String"},"content":{"rendered":"<p>Given a string, find the first non-repeating character in it and return it&#8217;s index. If it doesn&#8217;t exist, return -1.<\/p>\n<p><b>Examples:<\/b><\/p>\n<pre>s = \"leetcode\"\r\nreturn 0.\r\n\r\ns = \"loveleetcode\",\r\nreturn 2.\r\n<\/pre>\n<p><b>Note:<\/b>\u00a0You may assume the string contain only lowercase letters.<\/p>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution:\r\n    def firstUniqChar(self, s):\r\n        &quot;&quot;&quot;\r\n        :type s: str\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        a = dict()\r\n        index = 0\r\n        while index &lt; len(s):\r\n            if not isinstance(a.get(s[index], False), bool):\r\n                a[s[index]] = -2\r\n            else:\r\n                a[s[index]] = index\r\n            index += 1\r\n        result = -1\r\n        for key, value in a.items():\r\n            if value != -2:\r\n                if result != -1:\r\n                    result = min(result, value)\r\n                else:\r\n                    result = value\r\n        return result\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a string, find the first non-repeating character <a href=\"https:\/\/www.ccagml.com\/?p=176\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">387. First Unique Character in a String<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/176"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=176"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/176\/revisions"}],"predecessor-version":[{"id":177,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/176\/revisions\/177"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}