 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find total number of strings, that contains one unique characters in Python
Suppose we have a string s of lowercase letters, we have to find the total number of substrings that contain one unique character.
So, if the input is like "xxyy", then the output will be 6 as substrings are [x, x, xx, y, y, yy]
To solve this, we will follow these steps −
- total := 0
- previous := blank string
- for each character c in s, do- if c is not same as previous, then- previous := c
- temp := 1
 
- otherwise,- temp := temp + 1
 
- total := total + temp
 
- if c is not same as previous, then
- return total
Let us see the following implementation to get better understanding −
Example
class Solution:    def solve(self, s):       total = 0       previous = ''       for c in s:          if c != previous:             previous = c             in_a_row = 1          else:             in_a_row += 1             total += in_a_row       return total ob = Solution() print(ob.solve("xxyy"))  Input
"xxyy"
Output
6
Advertisements
 