-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.py
168 lines (134 loc) · 3.49 KB
/
strings.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Replace character in a string
# def replace(str, char1, char2):
# newStr = ""
# for x in str:
# if x == char1:
# newStr += char2
# else:
# newStr += x
# return newStr
# str = "fsafsavxz"
# str = replace(str, 's', 'd')
# print(str)
# count vowels, consonants, digits and symbols in a string
def countInString(str):
v = c = d = s = 0
str = str.lower()
for x in str:
if x >= 'a' and x <= 'z':
if x == 'a' or x == 'e' or x == 'i' or x == 'o' or x == 'u':
v += 1
else:
c += 1
elif x >= '0' and x <= '9':
d += 1
else:
s += 1
return v, c, d, s
# str = input()
# v, c, d, s = countInString(str)
# print(v, c, d, s)
# Check Permutation
def isPermutation(str1, str2):
if len(str1) != len(str2):
return False
# Sum of ASCII values approach
# sum1, sum2 = 0, 0
# for x in str1:
# sum1 += ord(x)
# for x in str2:
# sum2 += ord(x)
# if sum1 == sum2:
# return True
# return False
# counting frequency of each char
frequency = [0] * 256
for x in str1:
ch = ord(x)
frequency[ch] += 1
for x in str2:
ch = ord(x)
frequency[ch] -= 1
for i in range(256):
if frequency[i] != 0:
return False
return True
# str1 = input()
# str2 = input()
# print(isPermutation(str1, str2))
# Remove Consecutive Duplicates
def removeconsDup(string):
start, end = 0, len(string)
newStr = ""
while start < end:
uniqueChar = string[start]
uniqueCharIndex = start+1
while (uniqueCharIndex < end and string[uniqueCharIndex] == uniqueChar):
uniqueCharIndex += 1
newStr += uniqueChar
start = uniqueCharIndex
return newStr
# string = input()
# string = removeconsDup(string)
# print(string)
# Reverse Each Word
def reverseEachWord(string):
# string = string.split()
# newStr = ""
# for ele in string:
# ele = ele[::-1]
# newStr += ele + " "
# return newStr
return " ".join(x[::-1] for x in string.split())
# string = input()
# string = reverseEachWord(string)
# print(string)
# Remove character
def removeCharacter(string, ch):
# return string.replace(ch, '')
newStr = ""
for ele in string:
if ele != ch:
newStr += ele
return newStr
# string = input()
# ch = input()
# string = removeCharacter(string, ch)
# print(string)
# Highest Occuring Character
def highestOccuringChar(string):
freq = [0] * 256
maxFreq = 0
for i in range(len(string)):
freq[ord(string[i])] += 1
maxFreq = max(maxFreq, freq[ord(string[i])])
answer = '\0'
for i in range(len(string)):
if maxFreq == freq[ord(string[i])]:
answer = string[i]
break
return answer
# string = input()
# string = highestOccuringChar(string)
# print(string)
# Compress The String
def compress(string):
count = 1
newStr = ""
for i in range(len(string)-1):
if string[i] == string[i+1]:
count += 1
else:
if count > 1:
newStr += string[i] + str(count)
else:
newStr += string[i]
count = 1
if count > 1:
newStr += string[len(string)-1] + str(count)
else:
newStr += string[len(string)-1]
return newStr
string = input()
string = compress(string)
print(string)