-
Notifications
You must be signed in to change notification settings - Fork 0
/
MGM-finduniqsubstrings.py
88 lines (49 loc) · 1.84 KB
/
MGM-finduniqsubstrings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Task Description
# Given a string, how many different substrings exist in it that have no repeating characters? Two substrings are considered different if they have a different start or end index.
# Example
# s = "abac"
# The substrings that have no repeating characters in them are "a", "b", "a", "c", "ab", "ba", "ac", and "bac". Note that "aba" and "abac" do not qualify because the character 'a' is repeated in them. Also note that two substrings, "a" and "a", both qualify because their start indices are different: s[0] and s[2]. There are 8 substrings that have no repeating characters.
# Function Description
# Complete the function findSubstrings in the editor below.
# findSubstrings has the following parameter:
# string s: the given string
# Returns
# int: the number of substrings in s that have no repeating characters
# Constraints
# 1 ≤ length of s ≤ 105
# s consists of only lowercase English letters, ascii['a'-'z'].
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'findSubstrings' function below.
#
# The function is expected to return an INTEGER.
# The function accepts STRING s as parameter.
#
def findSubstrings(s):
# Time complexity : O(N)
count = 0
l = len(s)
uniqueStrs = []
alphaCount = [0]*26
i, j= 0,0
while(i<l):
if (j < l and alphaCount[ord(s[j]) - ord('a')] == 0):
alphaCount[ord(s[j]) - ord('a')] += 1
count += (j - i + 1)
j+=1
uniqueStrs.append(s[i:j])
else:
alphaCount[ord(s[i]) - ord('a')] -= 1
i+=1
return count
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
result = findSubstrings(s)
fptr.write(str(result) + '\n')
fptr.close()