在字符串中查找第一个不重复的字符

时间:2013-01-24 21:42:23

标签: python algorithm

我读了一份求职面试问题,为下面写了一些代码:

  

编写一个有效的函数来查找第一个非重复字符   一个字符串。例如,“total”中的第一个非重复字符是   'o'和“teeter”中的第一个非重复字符是'r'。讨论   算法的效率。

我在Python中提出了这个解决方案;但是,我确信有更好的方法可以做到这一点。

word="googlethis"
dici={}

#build up dici with counts of characters
for a in word:
    try:
        if dici[a]:
            dici[a]+=1
    except:
        dici[a]=1

# build up dict singles for characters that just count 1 

singles={}
for i in dici:
    if dici[i]==1:
        singles[i]=word.index(i)

#get the minimum value

mini=min(singles.values())

#find out the character again iterating...

for zu,ui in singles.items():
    if ui==mini:
        print zu 

是否有更简洁有效的答案?

22 个答案:

答案 0 :(得分:8)

In [1033]: def firstNonRep(word):
   ......:     c = collections.Counter(word)
   ......:     for char in word:
   ......:         if c[char] == 1:
   ......:             return char
   ......:         

In [1034]: word="googlethis"

In [1035]: firstNonRep(word)
Out[1035]: 'l'

编辑:如果您想在不使用Counter等帮助程序的情况下实现相同的功能:

def firstNonRep(word):
    count = {}
    for c in word:
        if c not in count:
            count[c] = 0
        count[c] += 1
    for c in word:
        if count[c] == 1:
            return c

答案 1 :(得分:2)

from collections import defaultdict
word="googlethis"
dici=defaultdict(int)

#build up dici with counts of characters
for a in word:
    if dici[a]:
        dici[a]+=1
for a in word:
    if didic[a] < 2:
        return a

那不行吗?

答案 2 :(得分:2)

更优雅的方式

def NotRepeatingCharacter(s):
    for c in s:
        if s.find(c) == s.rfind(c):
            return c
    return '_'

答案 3 :(得分:2)

sorted(word,key=lambda x:(word.count(x),word.index(x)) )[0]

我想 或者DSM也是consice

next(c for c in word if word.count(c) == 1) 

效率稍高

>>> word = "teeter"
>>> sorted(word,key=lambda x:(word.count(x),word.index(x)) )[0]
'r'
>>> word = "teetertotter"
>>> sorted(word,key=lambda x:(word.count(x),word.index(x)) )[0]
'o'
>>> word = "teetertotterx"
>>> sorted(word,key=lambda x:(word.count(x),word.index(x)) )[0]
'o'

答案 4 :(得分:1)

问题:字符串中的第一个非重复字符

方法1:制作计数数组

a = "GGGiniiiiGinaaaPrrrottiijayi"
count = [0]*256
for ch in a: count[ord(ch)] +=1
for ch in a :
    if( count[ord(ch)] == 1 ):
        print(ch)
        break

方法2:列表理解

# 1st  non repeating character
pal = [x for x in a if a.count(x) == 1][0]
print(pal)

方法3:字典

d={}
for ch in a: d[ch] = d.get(ch,0)+1
aa = sorted(d.items(), key = lambda ch  :ch[1])[0][0]
print(aa)

答案 5 :(得分:0)

由于我们需要第一个非重复字符,最好从集合中使用OrderedDict,因为dict不保证键的顺序

from collections import OrderedDict
dict_values = OrderedDict()
string = "abcdcd"

for char in string:
   if char in dict_values:
   dict_values[char] += 1
else:
    dict_values[char] = 1

for key,value in dict_values.items():
    if value == 1:
       print ("First non repeated character is %s"%key)
       break
    else:
       pass

答案 6 :(得分:0)

简单的Python3解决方案

def findFirstUniqueChar(s):
    d = {}
    for c in s:
        if c in d:
            d[c] += 1
        else:
            d[c] = 1

    for c in s:
        if d[c] == 1:
            return s.index(c)
    return -1

答案 7 :(得分:0)

# I came up with another solution but not efficient comment?
str1 = "awbkkzafrocfbvwcqbb"
list1 = []
count = 0
newlen = len(str1)
find = False


def myfun(count, str1, list1, newlen):

    for i in range(count, newlen):

        if i == 0:

            list1.append(str1[i])

        else:
            if str1[i] in list1:
                str1 = str1.translate({ord(str1[i]): None})
                print(str1)
                newlen = len(str1)
                count =0
                i = count
                list1.pop()
                myfun(count,str1,list1,newlen)
            else:
                pass

    if str1.find(list1[0], 1, len(str1)) != -1 :
        pass
    else:
        print(list1[0]+" is your first non repeating character")
        exit()


myfun(count, str1, list1, newlen)

答案 8 :(得分:0)

input_str = "interesting"
#input_str = "aabbcc"
#input_str = "aaaapaabbcccq"


def firstNonRepeating(param):
    counts = {}        
    for i in range(0, len(param)):

    # Store count and index repectively
        if param[i] in counts:
            counts[param[i]][0] += 1
        else:
            counts[param[i]] = [1, i]

    result_index = len(param) - 1
    for x in counts:
        if counts[x][0] == 1 and result_index > counts[x][1]:
            result_index = counts[x][1]
    return result_index

result_index = firstNonRepeating(input_str)
if result_index == len(input_str)-1:
    print("no such character found")
else:
    print("first non repeating charater found: " + input_str[result_index])

答案 9 :(得分:0)

/**
 * 
 * @param elements
 * @return
 */
private int firstRepeatedElement(int[] elements) {
    int firstRepeatedElement = -1;
    if(elements!=null && elements.length>0) {
        Set<Integer> setOfElements = new HashSet<>();
        for(int i=elements.length-1;i>=0;i--){
            if(setOfElements.contains(elements[i])) {
                firstRepeatedElement = elements[i];
            } else {
                setOfElements.add(elements[i]);
            }
        }
    }
    return firstRepeatedElement;
}


private int  firstNonRepeatedHashSet(int [] elements)  {
    int firstNonRepatedElement = -1;
    Set<Integer> hashOfElements = new HashSet<>();
    if(elements!=null && elements.length>0) {
        for(int i=elements.length-1;i>=0;i--) {
            if(!hashOfElements.contains(elements[i])) {
                hashOfElements.add(elements[i]);
                firstNonRepatedElement = elements[i];
            }
        }
    }
    return firstNonRepatedElement;
}


/**
 * @param args
 */
public static void main(String[] args) {
    FirstRepeatingAndNonRepeatingElements firstNonRepeatingElement = 
            new FirstRepeatingAndNonRepeatingElements();
    int[] input = new int[]{1,5,3,4,3,5,6,1};

    int firstRepeatedElement = firstNonRepeatingElement.
            firstRepeatedElement(input);

    System.out.println(" The First  Repating Element is "
            + firstRepeatedElement);


    int firstNonRepeatedElement = firstNonRepeatingElement.
            firstNonRepeatedHashSet(input);

    System.out.println(" The First Non Repating Element is "
            + firstNonRepeatedElement);

}

public class FirstRepeatingAndNonRepeatingElements {

package Mypack;
public class ClassAA{
public ClassAA()
{
    System.out.println("class A's constructor is called.");
}
public void testA()
{
    System.out.println("class A's methos is called");
}
}

答案 10 :(得分:0)

def firstNotRepeatingCharacter(s):

对于c中的c:

如果s.find(c)== s.rfind(c):

返回c

返回'_'

答案 11 :(得分:0)

def non_repeating(given_string):
    try:
        item = [x for x in given_string[:] if given_string[:].count(x) == 1][0]
    except:
        return None
    else:
        return item

# NOTE: The following input values will be used for testing your solution.
print non_repeating("abcab") # should return 'c'
print non_repeating("abab") # should return None
print non_repeating("aabbbc") # should return 'c'
print non_repeating("aabbdbc") # should return 'd'

答案 12 :(得分:0)

def firstnonrecur(word):

    for c in word:
        if word.count(c) == 1:
            print(c)
            break


firstnonrecur('google')

这个怎么样?

答案 13 :(得分:0)

以下代码仅遍历字符串一次。对于这个问题,这是一个简单而容易的解决方案,但同时空间复杂性也会受到影响。

def firstNonRepeating(a):
    inarr = []
    outarr = []

    for i in a:
        #print i
        if not i in inarr:
            inarr.append(i)
            outarr.append(i)
        elif i in outarr:
            outarr.remove(i)
        else:
            continue
    return outarr[0]

a = firstNonRepeating(“Your String”)
print a

答案 14 :(得分:0)

获得解决方案的三行python代码:

word="googlethis"
processedList = [x for x in word if word.count(x)==1]
print("First non-repeated character is: " +processedList[0])

或者,

word="googlethis"
print([x for x in word if word.count(x)==1][0])

答案 15 :(得分:0)

在Java中, 1)创建字符计数hashmap。   对于每个字符,如果字符中没有存储值,请将其设置为1。   否则将字符的值增加1。

2)然后我扫描了每个角色的字符串。            如果hashmap中的计数为1,则返回字符。            如果没有字符计数为1,则返回null。

package com.abc.ridhi;
import java.util.HashMap;
import java.util.Scanner;

public class FirstNonRepeated {

    public static void main(String[] args) {
        System.out.println(" Please enter the input string :");
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        char c = firstNonRepeatedCharacter(s);
        System.out.println("The first non repeated character is :  " + c);
    }

    public static Character firstNonRepeatedCharacter(String str) {
    HashMap<Character, Integer> map1 = new HashMap<Character, Integer>();
        int i, length;
        Character c;
        length = str.length(); 
        // Scan string and build hashmap
        for (i = 0; i < length; i++) {
            c = str.charAt(i);
            if (map1.containsKey(c)) {
                // increment count corresponding to c
                map1.put(c, map1.get(c) + 1);
            } else {
                map1.put(c, 1);
            }
        }
        // Search map in order of string str

        for (i = 0; i < length; i++) {
            c = str.charAt(i);
            if (map1.get(c) == 1){
                return c;
        }
        }
        return null;
    }
}

答案 16 :(得分:0)

的Python; O(N + N)我认为。

def find_first_non_occuring(test_str):
    results = {}
    for letter in test_str:
        if letter in results.keys():
            if results[letter] == 1:
                results[letter] = results[letter]+1
        else:
            results[letter] = 1 

    for letter in test_str:
        if results[letter] is 1:
            return letter
test_str = 'afixuboshafe fafd weagdgdg'
print find_first_non_occuring(test_str)

答案 17 :(得分:0)

我认为最坏的情况是O(n ^ 2)但对我来说很清楚:

def firstNonRep(word):
    """the first non-repeating character in a string: "ABCA" -> B """
    for (i, c) in enumerate(word):
        residual = word[i+1:]
        if not c in residual:
            return c

答案 18 :(得分:0)

这里的想法是用一些默认值初始化一个数组,例如0.当你遇到字符串中的特定字符时,你只需使用你最初定义的数组中该字符的ASCII值来增加计数器。

在传递字符串时,必须更新数组中字符的相应计数器。现在需要再次遍历数组,并检查是否有任何等于1的值,如果存在 - 只需将该字符作为给定字符串中的第一个非重复字符返回。

class FindFirstUniqueChar
{
    private static char ReturnFirstUniqueChar(string sampleString)
    { 
        // Initialize a sample char array and convert your string to char array.           
        char[] samplechar = sampleString.ToCharArray();

        // The default array will have all value initialized as 0 - it's an int array. 
        int[] charArray = new int[256];

        // Go through the loop and update the counter in respective value of the array 
        for (int i = 0; i < samplechar.Length; i++)
        {
          charArray[samplechar[i]] = charArray[samplechar[i]] + 1;
        }

        // One more pass - which will provide you the first non-repeated char.
        for (int i = 0; i < charArray.Length; i++)
        {

            if (charArray[samplechar[i]] == 1)
            {
                return samplechar[i];
            }
        }

        // Code should not reach here. If it returns '\0'
        // that means there was no non-repeated char in the given string. 
        return '\0';
    }

    static void Main(string[] args)
    {
        Console.WriteLine("The First Unique char in given String is: " +                         
                          ReturnFirstUniqueChar("ABCA"));
        Console.ReadLine();
    }
}

class FindFirstUniqueChar { private static char ReturnFirstUniqueChar(string sampleString) { // Initialize a sample char array and convert your string to char array. char[] samplechar = sampleString.ToCharArray(); // The default array will have all value initialized as 0 - it's an int array. int[] charArray = new int[256]; // Go through the loop and update the counter in respective value of the array for (int i = 0; i < samplechar.Length; i++) { charArray[samplechar[i]] = charArray[samplechar[i]] + 1; } // One more pass - which will provide you the first non-repeated char. for (int i = 0; i < charArray.Length; i++) { if (charArray[samplechar[i]] == 1) { return samplechar[i]; } } // Code should not reach here. If it returns '\0' // that means there was no non-repeated char in the given string. return '\0'; } static void Main(string[] args) { Console.WriteLine("The First Unique char in given String is: " + ReturnFirstUniqueChar("ABCA")); Console.ReadLine(); } }

我只提供了示例代码。它不包括错误检查和边缘情况。 对于那些有兴趣知道给定算法的时间复杂性的人 - 它是 O(N)+ O(N)= O(2N),几乎为O(N)。它确实使用了额外的内存空间。 欢迎您的反馈。

答案 19 :(得分:0)

我的解决方案。我不能说它的效率如何;我认为它在n ^ 2次运行。

>>> def fst_nr(s):
...     collection = []
...     for i in range(len(s)):
...             if not s[i] in collection and not s[i] in s[i+1:]:
...                     return s[i]
...             else:
...                     collection+=[s[i]]
... 
>>> fst_nr("teeter")
'r'
>>> fst_nr("floccinaucinihilipilification")
'u'
>>> fst_nr("floccinacinihilipilification")
'h'
>>> fst_nr("floccinaciniilipilification")
'p'
>>> fst_nr("floccinaciniiliilification")
't'
>>> fst_nr("floccinaciniiliilificaion")
>>> 

对于一个不起眼的Stack noob有什么建议吗?

答案 20 :(得分:-1)

str = "aabcbdefgh"
lst = list(str)

for i in [(item, lst.count(item)) for item in set(lst)]:
    if i[1] == 1:
        print i[0],

答案 21 :(得分:-1)

print("%s %s" % (weight_kg,"kg"))

通过使用 LinkedHashset,我们可以获得插入顺序。