簡體   English   中英

獲取每個名稱中每個不重復字母的最大數量

[英]get the maximum number of each non-repeating letter in each name

在這段代碼中,我希望用戶輸入一個整數,如n並輸入n名稱。 我需要找到用戶輸入的每個名稱中不重復字母的數量,並獲得這些數字之間的最大值並打印最大結果,即具有最多不重復的名稱的非重復字母的數量。重復字母。

我在必須獲得每個名稱中每個非重復字母的最大數量的部分存在問題,但我不知道該怎么做:

n = int(input())
for i in range(n):
    s = input()
    t = ''
    for ch in s:
        if ch not in t:
            t += ch

有兩組跟蹤一次或多次出現的字母

# your code goes here
n = int(input())
maxc_ = 0
word = ''
for i in range(n):
    s = input()
    count =0
    seen_once = set()
    seen_more = set()
    for ch in s:
        if ch not in seen_more:
            if ch not in seen_once:
                seen_once.add(ch)
                count +=1
            else:
                seen_once.remove(ch)
                seen_more.add(ch)
                count-=1
    if maxc_<count:
        maxc_ = count
        word = s

print(maxc_, word)

你可以試試這個:

n = int(input())
q=[]
for i in range(n):
    s = input()
    b=0
    for ch in s:
        t=0
        for j in range(0, len(s)):
            if ch == s[j]:
                t=t+1
        if t==1:
            b=b+1
    q.append(b)
print("The maximum number of unrepeated letters is:", max(q))

希望它有效:)

def solve():
    # solution goes here
    n = int(input())
    #to store the maximum non-repeating letter count
    maximum = 0 
    for i in range(n):
        s = input()
        #to store the letter count appears only once in a name
        count = 0 

        #to store the letter count of each name(size is 26 because we have 26 letters in English Alphabets)
        nameLetterCount = [0]*26 

        for ch in s:
            if(ord(ch) < 97):
                #convating all in lower case letter(as name can have mixed case letters Upper/Lower)
                ch = chr(ord(ch) + 32) 
            #storing the alphbet count
            nameLetterCount[ord(ch) - ord('a')] += 1 

        for i in range(26):
            if(nameLetterCount[i] == 1):
                #update the currrent name letter count
                count += 1 

        maximum = max(maximum, count)
    print(maximum)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM