首页 > 试题广场 >

找出字符串中第一个只出现一次的字符

[编程题]找出字符串中第一个只出现一次的字符
  • 热度指数:200374 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
找出字符串中第一个只出现一次的字符


数据范围:输入的字符串长度满足



输入描述:

输入一个非空字符串



输出描述:

输出第一个只出现一次的字符,如果不存在输出-1

示例1

输入

asdfasdfo

输出

o
python 
# coding: utf-8
def func(s1):
    for i in s1:
        if s1.count(i) == 1:
            print i
            break
    else:
        print -1
if __name__ == "__main__":
    import sys
    line1 = sys.stdin.readline().strip()
    func(line1)


发表于 2022-04-13 00:01:03 回复(0)
while True:
    try:
        str1 = input()
        str2 = ""#用于去除重复项,不能用set()因为会打乱而这个要求第一个出现一次
        for i in range (len(str1)):
            if str1[i] not in str2:
                str2 = str2 + str1[i]#当为第一次出现时添加项
        counter = 0
        succ = 0
        for i in range (len(str2)):
            counter = str1.count(str2[i])#按序查找出现次数
            if counter == 1:
                print(str2[i])
                succ = 1
                break#一旦出现第一个只出现一次的跳出循环
        if succ == 0:#没有时返回-1
            print(-1)
    except:
        break

发表于 2021-06-29 23:13:16 回复(0)
import sys


def func(s):
    if len(s) <= 1:
        return s
    else:
        for i in range(len(line)):
            if line.count(line[i]) == 1:
                return line[i]
    return -1
 
for line in sys.stdin:
    line = line.strip()
    print(func(line))

发表于 2021-04-29 13:30:04 回复(0)
while True:
    try:
        str0 = input()
        dict0 = dict()
        flag = -1 #太酷了吧
        for s in str0:
            dict0[s] = dict0.get(s,0) + 1
        for key,value in dict0.items():
            if value == 1:
                flag = key
                break
        print(flag)
    except:
        break
#初始化falg为-1,若存在,则改变;
发表于 2021-04-21 23:31:07 回复(0)

Python的两种解法

第一种用字典,统计好每个字符的个数之后,从字典里按顺序找到个数为1的字符输出就可以了。
用字典处理的方法可以用的题目相对而言广泛一点,可以用来处理更复杂一点的问题。

while True:
    try:
        s = list(input())
        dic = {}
        char = -1
        for i in range(len(s)):
            if s[i] in dic:
                dic[s[i]] += 1
            else:
                dic[s[i]] = 1
        for i in dic:
            if dic[i] == 1:
                char = i
                break
        print(char)
    except EOFError:
        break

就这道题目而言,下面这种不用字典,直接count的方法更简单

while True:
    try:
        s = list(input())
        char = -1
        for i in s:
            if s.count(i) == 1:
                char = i 
                break
        print(char)
    except EOFError:
        break
发表于 2021-04-12 15:42:56 回复(0)
# 字典yyds
def cal(string):
    if string == '':
        print(-1)
        return
    has = {}
    for i in string:
        if i in has:
            has[i] += 1
        else:
            has[i] = 1
    for key in has:
        if has[key] == 1:
            print(key)
            return
    print(-1)
    return


while True:
    try:
        string = input()
        cal(string)
    except:
        break
发表于 2021-04-05 15:37:59 回复(0)
while True:
    try:
        str = input()
        stat = []
        for x in str:
            stat.append([x, str.count(x)])
        once = list(filter(lambda x: x[1] == 1, stat))
        if len(once) < 1:
            print(-1)
        else:
            print(once[0][0])
    except EOFError:
        break

发表于 2021-03-25 01:29:10 回复(0)
while True:
    try :
        inp = input()
        mydict = {}
        
        for i in inp:
            value = inp.count(i)
            if value == 1:
                print(i)
                control = True
                break
            else :
                control = False
        if not control :
            print(-1)
    except :
        break

发表于 2021-03-15 11:49:04 回复(0)
本来用字典,统计字典的出现此处,后面发现不对,因为字典是乱序的!!!!
s.count(i)  统计字符的出现次数

if __name__ == '__main__':
    while True:
        try:
            s= raw_input()
            s1=[]
            a={}
            flag= 0
            for i in s:
                if s.count(i)==1:
                    s1.append(i)
            if len(s1)==0:
                print -1
            else:
                print s1[0]
        except:
            break

发表于 2021-03-12 22:31:02 回复(0)
哈希表存储,两次便利 复杂度O(n)
while True:
    try:
        s = input().strip()
        d = {}
        for i in s:
            d[i] = d.get(i, 0) + 1
        count = 0
        for i in s:
            if d[i] == 1:
                print(i)
                count += 1
                break
        if not count:
            print(-1)
    except:
        break


发表于 2021-02-20 19:28:33 回复(0)
while True:
    try:
        num=input()
        flag=0
        for i in num:
            if(num.count(i)==1):
                print(i)
                flag=1
                break              
        if not flag:print('-1')
    except:break
           
发表于 2021-01-27 15:48:33 回复(0)
while True:
    try:
        string = input()
        d={}
        key=""
        for i in string:
            if i in d:
                d[i] += 1
            else:
                d[i] = 1
        d_keys = list(d.keys())
        for j in d_keys:
            if d[j] ==1:
                key = j
                break
            else:
                continue
        if key =="":
            print("-1")
        else:
            print(key)
    except:
        break
发表于 2021-01-23 13:34:04 回复(0)
while True:
    try:
        s = input();sign = 0
        for i in s:
            if s.count(i)==1:
                sign = 1
                ss = i
                break
        print(ss) if sign != 0 else print(-1)
    except:
        break
发表于 2021-01-12 15:11:28 回复(0)
import sys
from collections import OrderedDict


for s in sys.stdin:
    s = s.strip()
    repeated_chars, once_chars = set(), OrderedDict()
    for c in s:
        if c in once_chars:
            once_chars.pop(c)
            repeated_chars.add(c)
        elif c not in repeated_chars:
            once_chars[c] = 1
    if not once_chars:
        print(-1)
    else:
        k ,v = once_chars.popitem(last=False)
        print(k)
            

发表于 2020-12-17 21:15:29 回复(0)
while True:
    try:
        s = input()
        res = []
        for i in s:
            if s.count(i) == 1:
                res.append(i)
        if res == []:
            print(-1)
        else:
            print(res[0])
    except:
        break

发表于 2020-12-14 16:32:43 回复(0)
while True:
    try:
        a = input().strip()
        for i in a:
            if a.count(i) == 1:
                print(i)
                break
        else:
            print(-1)
    except:
        break

发表于 2020-12-04 22:15:52 回复(1)
# 2020年11月17日15:06:50
while True:
    try:
        string = input()
        for i in range(len(string)):
            if string.count(string[i])==1:
                print(string[i])
                break
            elif i==len(string)-1:
                print(-1)
    except:
        break
        

发表于 2020-11-17 15:10:32 回复(0)
from collections import defaultdict
while True:
    try:
        s = input()
        dd = defaultdict(list)
        for i, v in enumerate(s):
            if not dd[v]:
                dd[v].extend([1, i])
            else:
                dd[v][0] += 1
        # 先按照出现的次数排序,再按照出现的先后排序
        dd = sorted(dd.items(), key=lambda x:(x[1][0], x[1][1]))
        if dd[0][1][0] == 1:
            print(dd[0][0])
        else:
            print(-1)
    except:
        break

发表于 2020-09-29 16:14:39 回复(0)

问题信息

难度:
41条回答 54803浏览

热门推荐

通过挑战的用户

查看代码