Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find total number of strings, that contains one unique characters in Python
When working with strings, we often need to count substrings that contain only one unique character. This means finding all contiguous sequences where all characters are the same.
So, if the input is like "xxyy", then the output will be 6 as the valid substrings are: ["x", "x", "xx", "y", "y", "yy"].
Algorithm
To solve this problem, we follow these steps ?
- Initialize total count to 0
- Track the previous character to detect character changes
- For each character in the string:
- If character differs from previous, start a new sequence
- Otherwise, extend the current sequence
- Add the sequence length to total count
- Return the total count
Example
Let's implement this solution ?
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
# Test the solution
ob = Solution()
result = ob.solve("xxyy")
print(f"Input: 'xxyy'")
print(f"Output: {result}")
# Let's trace through the algorithm
s = "xxyy"
print(f"\nTracing through '{s}':")
total = 0
previous = ''
in_a_row = 0
for i, c in enumerate(s):
if c != previous:
previous = c
in_a_row = 1
else:
in_a_row += 1
total += in_a_row
print(f"Character '{c}': sequence length = {in_a_row}, total = {total}")
Input: 'xxyy' Output: 6 Tracing through 'xxyy': Character 'x': sequence length = 1, total = 1 Character 'x': sequence length = 2, total = 3 Character 'y': sequence length = 1, total = 4 Character 'y': sequence length = 2, total = 6
How It Works
The algorithm counts substrings by recognizing that for a sequence of n identical characters, there are n*(n+1)/2 possible substrings. However, we can calculate this incrementally:
- First character: contributes 1 substring
- Second identical character: contributes 2 substrings (itself + extending the previous)
- Third identical character: contributes 3 substrings
- And so on...
Alternative Implementation
Here's a more concise version without using a class ?
def count_unique_substrings(s):
if not s:
return 0
total = 0
count = 1
for i in range(len(s)):
if i == 0 or s[i] == s[i-1]:
total += count
count += 1
else:
count = 1
total += count
count += 1
return total
# Test with multiple examples
test_cases = ["xxyy", "aaa", "abc", ""]
for test in test_cases:
result = count_unique_substrings(test)
print(f"Input: '{test}' ? Output: {result}")
Input: 'xxyy' ? Output: 6 Input: 'aaa' ? Output: 6 Input: 'abc' ? Output: 3 Input: '' ? Output: 0
Conclusion
This algorithm efficiently counts substrings with unique characters by tracking consecutive character sequences. The time complexity is O(n) and space complexity is O(1), making it optimal for this problem.
