Leetcode notes First Unique Character in a String

Source: Internet
Author: User

Title Description:

Given a string, find the first non-repeating character in it and return it ' s index. If it doesn ' t exist, return-1.

Examples:

s = "Leetcode" return 0.s = "Loveleetcode", return 2.

Note:you may assume the string contain only lowercase letters.

My answer:

Main ideas:

First, turn the string into a list of letters, then iterate through the list letters to create a counting dictionary and an index dictionary, and then iterate through the alphabetical list to find the first occurrence of the index of 1 letters.

Error point:

1 Get Count Dictionary Uni and Index Dictionary ind, directly in Uni look for a value of 1 letters, so the reason for the error is that after the counting dictionary, the alphabetical order has changed, the resulting letter is not the first occurrence

2 ignore the non-existent situation, for the absence of the situation, that is, all counts are greater than 1, the method used here is a count greater than 1, if the last number equal to the length of the list indicates that there is no

3 is always written as a logical operation =, should be a double equals = =

Knowledge Points:

For index,t in enumerate (list) to traverse list values and indexes at the same time

For k,v in Uni.items () to traverse the key and value of the dictionary uni at the same time

Leetcode notes First Unique Character in a String

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.