-
Notifications
You must be signed in to change notification settings - Fork 0
/
beautifulStrings.py
62 lines (54 loc) · 1.26 KB
/
beautifulStrings.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
import math
import os
import random
import re
import sys
#
# Complete the 'beautifulStrings' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts STRING s as parameter.
#
# def beautifulStrings(s):
# Write your code here
# abdde
# abc
# aaaa
# 1111
# uniqStrings = {}
# counter = 0
# for i in range(0,len(s)):
# if(i != len(s)-1):
# for j in range(i+1,len(s)):
# if((s[:i]+s[i+1:j]+s[j+1:len(s)]) not in uniqStrings):
# uniqStrings[(s[:i]+s[i+1:j]+s[j+1:len(s)])] = 0
# counter += 1
# return counter
def min_operations_to_beautiful(s):
# ops = 0
# i = 0
# while i < len(s) - 1:
# diff = ord(s[i]) - ord(s[i+1])
# if diff in [0, 1, -1]:
# ops += 1
# i += 2 # skip the next character
# else:
# i += 1
# return ops
ops = 0
i = 0
while i < len(s) - 1:
diff = ord(s[i]) - ord(s[i+1])
if diff in [0,1,-1]:
ops += 1
i += 2
else:
i += 1
return ops
if __name__ == '__main__':
s = "bartqa"
# aaaaa
# 10101
# aabc
# 111
print(min_operations_to_beautiful(s))