Home > Net >  get the maximum number of each non-repeating letter in each name
get the maximum number of each non-repeating letter in each name

Time:07-17

In this code , I want the user to enter an integer number like n and enter n names. And I need to find the number of non-repeating letters in each name entered by the user and get the maximum between those numbers and print the maximum result, which is the number of non-repeating letters of the name that has the most non-repeating letters.

I have a problem in the part where I have to get the maximum number of each non-repeating letter in each name and I don't know how to do this:

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

CodePudding user response:

Have two set which keep track of letter which occur one time or multiple time

# 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)

CodePudding user response:

You can try this:

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))

Hope it works :)

CodePudding user response:

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)
  • Related