Python: Find two indices making a given string unhappy
Indices for Unhappy String
A string is happy if every three consecutive characters are distinct. Write a Python program to find two indices associated with a given string being unhappy.
Input: Python Output: None Input: Unhappy Output: [4, 5] Input: Find Output: None Input: Street Output: [3, 4]
Visual Presentation:
Sample Solution:
Python Code:
Sample Output:
Original string: Python Find two indices making the said string unhappy! None Original string: Unhappy Find two indices making the said string unhappy! [4, 5] Original string: Find Find two indices making the said string unhappy! None Original string: Street Find two indices making the said string unhappy! [3, 4]
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Python program to detect if any three consecutive characters in a string are not all distinct and return their indices.
- Write a Python program to slide a window of size 3 over a string and output the indices where duplicates occur.
- Write a Python program to use iteration and comparison to find the first occurrence of three characters with a duplicate, then return the indices of the violation.
- Write a Python program to implement a function that returns indices of the first “unhappy” triplet in a string or None if the string is happy.
Go to:
Previous: Find the sublist of numbers with only odd digits in increasing order.
Next: Find the index of the matching parentheses for each character in a given string.
Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.