找出字符串中第一个只出现一次的字符
数据范围:输入的字符串长度满足
#方法①——使用count()函数计算字符个数 while True: try: s = input() for i in s: if s.count(i) == 1: print(i) break else: print('-1') except: break #方法②——使用hash表个存储 S = input().strip() l_s = dict() for i in range(len(S)): #将字符按顺序存入hash表(字典、set均可) if S[i] not in l_s.keys(): #不在hash表中,则添加 l_s[S[i]] = 1 else: #在hash表中,则value+1 l_s[S[i]] += 1 flag = 0 for key in l_s.keys(): if l_s[key] == 1: #按hash表顺序读取,若其中value=1,则输出对应key,后直接跳出循环 print(key) flag = 1 break if flag == 0: #此情况适用于,上面hash表顺序执行后,无value=1情况(字符串中字符均重复),输出-1 print('-1') 以上解法仅供参考,欢迎提供新解法
a = input() dic = {} for i in a: if i not in dic: dic[i] = a.count(i) if dic[i] == 1: break if dic[i] == 1: print(i) else: print(-1)
s=input() def frist(s): for i in s: if s.count(i)==1: return i if frist(s): print(frist(s)) else: print(-1)
a=input() for one in a: if a.count(one)==1: print(one) break else: print(-1)
def first_str(n): for i in n: if n.count(i)==1: return i return -1 print(first_str(input()))
while True: try: str_in = input() # 记录只出现一次的字符有哪些 str2num = {} lst = [] for s in str_in: if s not in list(str2num.keys()): str2num[s] = 0 str2num[s]+=1 if s not in lst: lst.append(s) if not min(list(str2num.values()))==1: print(-1) else: for s in lst: if str2num[s] == 1: print(s) break break # 打印第一个只出现一次的字符,否则打印-1 except: break
while True: try: a = input() b = {} d = set() for i in range(len(a)): if a[i] not in d: if a[i] not in b: b[a[i]] = [i] else: del b[a[i]] d.add(a[i]) e = [] for j in b.values(): e.extend(j) if e: e.sort() print(a[e[0]]) else: print('-1') except: break
from collections import Counter while True: try: words = input() length = len(words) words_dict = Counter(words) if 1 in words_dict.values(): temp_list = [] for i in words_dict.keys(): if words_dict[i] == 1: temp_list.append(i) # temp_dict = {} # for i in range(length): # if words[i] in temp_list: # temp_dict[i] = words[i] # print(temp_dict[min(temp_dict.keys())]) for i in range(length): if words[i] in temp_list: print(words[i]) break else: print(-1) except: break
s=str(input()) l=[] for i in s: x=s.count(i) if x==1: l.append(i) if len(l)!=0: print(l[0]) else: print(-1)