程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

LeetCode-6104. Statistical asterisk_ Python

編輯:Python
  • Give you a string s , Every two consecutive vertical lines ‘|’ It's a couple . In other words , The first and the second ‘|’ It's a couple , The third and the fourth ‘|’ It's a couple , And so on .

  • Please return not between vertical line pairs ,s in ‘*’ Number of .

  • Be careful , Each vertical line ‘|’ Will just belong to a pair .

Example 1:

Input :s = “l|eet|co|*de|"
Output :2
explain : Characters not between vertical line pairs are bold and italicized , Get a string :"l|eet|c
o|*de|” .
The first and second vertical lines ‘|’ Characters between are not included in the answer .
meanwhile , The third and fourth vertical lines ‘|’ Characters between are not included in the answer .
There is a total of... Between vertical line pairs 2 asterisk , So we go back to 2 .

Example 2:

Input :s = “iamprogrammer”
Output :0
explain : In this case ,s There is no asterisk in . So back 0 .

Example 3:

Input :s = “yo|uar|e**|b|eau|tifu|l"
Output :5
explain : Characters to be considered are bold and italicized :"yo|uar|e
|b|e
**au|tifu|l” . There is a total of... Between vertical line pairs 5 asterisk . So we go back to 5 .

Tips :

1 <= s.length <= 1000
s Only lowercase letters , A vertical bar ‘|’ And asterisk ‘*’ .
s contain even numbers Vertical lines ‘|’ .

Program code

class Solution:
def countAsterisks(self, s: str) -> int:
count = 0
ans = 0
for i in s:
if i == '|':
count += 1
if count % 2 == 0 and i == '*':
ans += 1
return ans

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved