diff --git a/Problem Solving/Python/AlternatingCharacters.py b/Problem Solving/Python/AlternatingCharacters.py new file mode 100644 index 00000000..87bf01c4 --- /dev/null +++ b/Problem Solving/Python/AlternatingCharacters.py @@ -0,0 +1,77 @@ +#You are given a string containing characters A and B only. Your task is to change it into a string such that there are no matching adjacent characters. To do this, you are allowed to delete zero or more characters in the string. + +#Your task is to find the minimum number of required deletions. + +#For example, given the string s=AABAAB, remove an A at positions 0 and 3 to make s=ABAB in 2 deletions. + +#Function Description + +#Complete the alternatingCharacters function in the editor below. It must return an integer representing the minimum number of deletions to make the alternating string. + +#alternatingCharacters has the following parameter(s): +#s: a string + +#Input Format + +#The first line contains an integer q, the number of queries. +#The next q lines each contain a string s. + +#Constraints +#1<=q<=10 +#1<=|s|<=10^5 +#Each string will consist only of characters A and B + +#Output Format +#For each query, print the minimum number of deletions required on a new line. + +#Sample Input +#5 +#AAAA +#BBBBB +#ABABABAB +#BABABA +#AAABBB + +#Sample Output +#3 +#4 +#0 +#0 +#4 + +#!/bin/python3 + +import os + +# Complete the alternatingCharacters function below. +def alternatingCharacters(s): + l=[] + ct=0 + for ch in s: + l.append(ch) + a=[] + a=l + length= len(l) + for i in range(length): + for j in range(i+1, len(l)): + if(l[i] == l[j]): + a[j]=0 + else: + break + for i in a: + if(i==0): + ct+=1 + return ct +if __name__ == '__main__': + fptr = open(os.environ['OUTPUT_PATH'], 'w') + + q = int(input()) + + for q_itr in range(q): + s = input() + + result = alternatingCharacters(s) + + fptr.write(str(result) + '\n') + + fptr.close() diff --git a/Python/TriangleQuest2.py b/Python/TriangleQuest2.py index 1a2a9fd5..744c698e 100644 --- a/Python/TriangleQuest2.py +++ b/Python/TriangleQuest2.py @@ -75,7 +75,7 @@ for i in range(1,int(input())+1): print(([1,11,111,1111,11111,111111,1111111,11111111,111111111,1111111111][i-1])**2) -======= + for i in range(1,int(input())+1): print(([1,11,111,1111,11111,111111,1111111,11111111,111111111,1111111111][i-1])**2)