|
| 1 | +from typing import List, Optional, Set |
| 2 | + |
| 3 | + |
| 4 | +class Solution: |
| 5 | + def uniqueLetterString(self, s: str) -> int: |
| 6 | + permutations = ( |
| 7 | + set() |
| 8 | + ) # where we store the permutations which is [index started at, permutation] in STR (json stringify basically) |
| 9 | + self.generate_perms(s, permutations) # populates permutation array |
| 10 | + total_count = 0 # count of total unique characters |
| 11 | + print(permutations) # prints the permutations |
| 12 | + for each_perm in permutations: # for each permutation in permutations |
| 13 | + each_perm = each_perm.strip("][").split(", ") # converts it back to a list |
| 14 | + total_count += sum( |
| 15 | + [1 if each_perm[1].count(x) == 1 else 0 for x in set(each_perm[1])] |
| 16 | + ) # sums up the unique characters, UNIQUE MEANING IT ONLY APPEARS ONCE, AAB = 1 BECAUSE B OCCURS 1 WHILE A OCCURS 2 |
| 17 | + return total_count |
| 18 | + |
| 19 | + def generate_perms( |
| 20 | + self, s: str, permutations: List[str], current_sub: Optional[str] = "", offset=0 |
| 21 | + ) -> None: |
| 22 | + |
| 23 | + for i in range(len(s)): |
| 24 | + current_sub += s[i] # Choice |
| 25 | + if ( |
| 26 | + str([offset, current_sub]) not in permutations |
| 27 | + ): # Validation + recursive call if valid choice |
| 28 | + permutations.add( |
| 29 | + str([offset, current_sub]) |
| 30 | + ) # add current permutation to stack |
| 31 | + self.generate_perms( |
| 32 | + s[i + 1 :], permutations, current_sub, offset |
| 33 | + ) # generate next permutation |
| 34 | + current_sub = current_sub[1:] # move to next character in perm |
| 35 | + offset += 1 # increment offset due to index increasing in loop iteration |
| 36 | + |
| 37 | + |
| 38 | +if __name__ == "__main__": |
| 39 | + s = "DELQGVWNZKIJJPSXOVWWIZUXCEGWSQLESNSRBMKZARFPAXSVWQEZDENDAHNNIBHGHTFDLPGDLFXMIYRFNLMXHNPIFUAXINXPXLCTTJNLGGMKJIOEWBECNOFQPVCIKIAZMNGHEHFMCPWSMJTMGVSXTOGCGUYKFMNCGLCBRAFJLJVPIVDOLJBURULPGXBVDCEWXXXLTRMSHPKSPFDGNVOCZWDXJUWVNAREDOKTZMIUDKDQWWWSAEUUDBHMWZELOSBIHMAYJEMGZPMDOOGSCKLVHTGMETHUISCLJKDOQEWGVBULEMUXGTRKGXYFDIZTZWMLOFTCANBGUARNWQEQWGMIKMORVQUZANJNRNPMJWYLVHWKDFLDDBBMILAKGFROEQAMEVONUVHOHGPKLBPNYZFPLXNBCIFENCGIMIDCXIIQJWPVVCOCJTSKSHVMQJNLHSQTEZQTTMOXUSKBMUJEJDBJQNXECJGSZUDENJCPTTSREKHPRIISXMWBUGMTOVOTRKQCFSDOTEFPSVQINYLHXYVZTVAMWGPNKIDLOPGAMWSKDXEPLPPTKUHEKBQAWEBMORRZHBLOGIYLTPMUVBPGOOOIEBJEGTKQKOUURHSEJCMWMGHXYIAOGKJXFAMRLGTPNSLERNOHSDFSSFASUJTFHBDMGBQOKZRBRAZEQQVWFRNUNHBGKRFNBETEDJIWCTUBJDPFRRVNZENGRANELPHSDJLKVHWXAXUTMPWHUQPLTLYQAATEFXHZARFAUDLIUDEHEGGNIYICVARQNRJJKQSLXKZZTFPVJMOXADCIGKUXCVMLPFJGVXMMBEKQXFNXNUWOHCSZSEZWZHDCXPGLROYPMUOBDFLQMTTERGSSGVGOURDWDSEXONCKWHDUOVDHDESNINELLCTURJHGCJWVIPNSISHRWTFSFNRAHJAJNNXKKEMESDWGIYIQQRLUUADAXOUEYURQRVZBCSHXXFLYWFHDZKPHAGYOCTYGZNPALAUZSTOU" |
| 40 | + sol = Solution() |
| 41 | + print(sol.uniqueLetterString(s)) |
0 commit comments