Given a String, check whether all the lowercase letters are present exactly once. If present, then output 0. If some letters are missing then print the missing letters in the form of a string in Lexicographic order. If all the letters are present and some of them are repeated more than once then print the sum of ASCII characters of the letters which occurred more than once.
The input given is a string that can have -
- All lowercase letters without duplicates
- All lowercase letters with duplicates
- Not all lower case letters with or without duplicates
The given string can be classified as the three by removing duplicates and then checking the length of the obtained string.
- If the length of the string is 26 after removing its duplicates, then it may belong to the first or the second category.
- Otherwise, falls under the third Category. This can be easily checked using Set Interfaces.
Let us take the third case where the length of the string without duplicates is less than 26. In such a case, we need to print a string that has all the missing alphabets. We can keep track of all letters that appeared in the string using a boolean array or a Dictionary. Then we just output the missing characters by iterating over the boolean array or Dictionary.
To solve for strings of type 1 or 2, finding the difference between the sum of ASCII values of the letters in String and the sum of ASCII values of all alphabets (
[a-z]) will do the job.
def res(a): x = ''.join((sorted(set(a)))) y = ''.join([chr(i) for i in range(97, 123)]) sum = 0 if (len(a) < 26): return((''.join(sorted(set(y) - set(x))))) else: for i in (a): sum += ord(i) return (sum - 2847) # 2847 Signfies the sum of ASCII values of characters [a-z] lis= for i in range(int(input())): lis.append(res(input())) for i in lis: print(i)