Python tutorial – The first unique character in a string

Topic:

Given a string, find its first non-repeating character and return its index. If none exists, -1 is returned.

Case study:

s = "leetcode"Returns 0.s ="loveleetcode"To return to the 2.Copy the code

Note: You can assume that this string contains only lowercase letters.

Answer:

Find the index of the letter with the frequency of 1.

You do it twice with a hash map. For the first traversal, the Hash Map Key is a letter and Value is the occurrence frequency. The second pass finds an alphabetic index with a frequency of 1.

Different from word frequency statistics, there are only 26 letters in total, so you can directly use ASii code table in lowercase letters from 97 to 122, directly use int array mapping. Build a map: Index ASii code values of lowercase letters and store values of occurrence frequency.

Hash mapping solution:

Java:

class Solution { public int firstUniqChar(String s) { char[] chars = s.toCharArray(); // Char array Map Map = new HashMap<>();for(Character c: chars) map.put(c, map.getOrDefault(c, 0) + 1); // Frequency statisticsfor (int i = 0; i < chars.length; i++) {

 if(map.get(chars[i])==1) returni; // Find the letter with word frequency 1 (occurs only once) and return its index}return -1;

 }

}Copy the code

Python:

class Solution:

 def firstUniqChar(self, s):

 count = collections.Counter(s)# This function is an integration function for word frequency statistics in Python's base library

 index = 0

 for ch in s:

 if count[ch] == 1:

 return index

 else:

 index += 1 

 return- 1Copy the code

Array mapping solution:

Java:

class Solution {

 public int firstUniqChar(String s) {

 char[] chars = s.toCharArray();

 int base = 97;

 int[] loc = new int[26];

 for (char c:chars) loc[c - base] += 1;

 for (int i = 0; i < chars.length; i++) {

 if(loc[chars[i]-base]==1) return i;

 }

 return -1;

 }

}Copy the code

There are no char types in Python’s underlying data structures, and forcing CHR (I) conversions will only make things less efficient



String function

Java:

Using Java string integration operation function to solve the problem, very clever, very efficient.

Among them:

IndexOf (): returns the indexOf the element when it first appears, or -1 if it does not

LastIndex (): Returns the index of the last occurrence of the element, or -1 if there is none

class Solution {

 public int firstUniqChar(String s) {

 int res = s.length();

 for (int i = 'a'; i <= 'z'; i++) {

 int firstIndex = s.indexOf((char)i);

 if (firstIndex == -1) continue;

 int lastIndex = s.lastIndexOf((char)i);

 if(firstIndex == lastIndex) {// If the index is the same twice, the letter must appear only once. //res is the smallest index of the letters that occur only once}}returnres == s.length() ? -1 : res; }}Copy the code

Everybody has what opinion or suggestion welcome message to point out! More Python tutorials will continue to be updated! We need to learn the course can also xiuyan or private letter!