일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 파이썬
- 캐치카페
- AI 경진대회
- 자연어처리
- dacon
- 백준
- leetcode
- Docker
- Real or Not? NLP with Disaster Tweets
- PYTHON
- programmers
- 편스토랑 우승상품
- hackerrank
- 우분투
- 코로나19
- Baekjoon
- ubuntu
- Kaggle
- 프로그래머스
- github
- 맥북
- 프로그래머스 파이썬
- 금융문자분석경진대회
- 더현대서울 맛집
- 편스토랑
- ChatGPT
- SW Expert Academy
- Git
- 데이콘
- gs25
- Today
- Total
솜씨좋은장씨
[HackerRank] String Manipulation : Alternating Characters (Python) 본문
[HackerRank] String Manipulation : Alternating Characters (Python)
솜씨좋은장씨 2020. 3. 26. 19:30
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 , the number of queries.
The next lines each contain a string .
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
Explanation
The characters marked red are the ones that can be deleted so that the string doesn't have matching consecutive characters.
Solution
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the alternatingCharacters function below.
def alternatingCharacters(s):
count = 0
for i in range(len(s)-1):
if s[i] == s[i+1]:
count = count + 1
return count
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[BaeKJoon] 2902번: KMP는 왜 KMP일까? (Python) (0) | 2020.03.28 |
---|---|
[leetCode] 557. Reverse Words in a String III (Python) (0) | 2020.03.27 |
[HackerRank] Sorting : Mark and Toys (Python) (0) | 2020.03.25 |
[leetCode] 929. Unique Email Addresses (Python) (0) | 2020.03.24 |
[BaeKJoon] 10844번: 쉬운 계단수 (Python) (0) | 2020.03.23 |