-2

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
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    `t` would be better as a set instead of a string – Tomerikoo Jul 17 '22 at 09:05
  • What do you mean by _non-repeating_: Does `a` in `aba` count as non-repeating? – Timus Jul 17 '22 at 11:22
  • 1
    Your problem has two steps: 1. count the amount of unique letters in a string. 2. do that in a loop and find the maximum. The second step is quite easy - keep a counter and record the maximum while you loop, or collect all results to a list and find the maximum of it. The first step is answered here already [List of all unique characters in a string?](https://stackoverflow.com/q/13902805/6045800) – Tomerikoo Jul 17 '22 at 12:18
  • The correct link is [How to get non-repeated elements in a list](https://stackoverflow.com/q/29860727/6045800) (answers are also valid for strings) – Tomerikoo Jul 17 '22 at 12:23

3 Answers3

0

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)
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
0

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

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