w3resource

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:

Python: Find two indices making a given string unhappy.
Python: Find two indices making a given string unhappy.

Sample Solution:

Python Code:

# License: https://bit.ly/3oLErEI

# Define a function named 'test' that takes a string 's' as input
def test(s):
    # Iterate through the characters of the string up to the second-to-last character
    for i in range(len(s) - 2):
        # Check if consecutive characters are the same, return the indices if true
        if s[i] == s[i + 1]:
            return [i, i + 1]
        # Check if characters with one character in between are the same, return the indices if true
        if s[i] == s[i + 2]:
            return [i, i + 2]

# Example 1
strs1 = "Python"
print("Original string:", strs1)
print("Find two indices making the string unhappy:")
print(test(strs1))

# Example 2
strs2 = "Unhappy"
print("\nOriginal string:", strs2)
print("Find two indices making the string unhappy:")
print(test(strs2))

# Example 3
strs3 = "Find"
print("\nOriginal string:", strs3)
print("Find two indices making the string unhappy:")
print(test(strs3))

# Example 4
strs4 = "Street"
print("\nOriginal string:", strs4)
print("Find two indices making the string unhappy:")
print(test(strs4))

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:

Flowchart: Python - Find two indices making a given string unhappy.

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.



Follow us on Facebook and Twitter for latest update.