PLAYPIAN - Editorial

Problem Link - Play Piano Practice Problem in

Problem Statement:

Two sisters, A and B, play the piano once per day, in any order. They record each session in a log, writing their name whenever they play. You are given a log and must check if it’s valid, meaning every day (pair of entries) has exactly one ‘A’ and one ‘B’.

Approach:

To solve this problem, we need to verify if each string represents valid log entries:

  • Each day will have only two entries
  • Each day must consist of exactly one ‘A’ and one ‘B’, so we need to check that every consecutive pair of characters contains exactly one ‘A’ and one ‘B’.
  • For each day either ‘AB’ will be valid or ‘BA’ will be valid.
  • Steps: Iterate through the array and check if for one day the adjacent element is equal or not. If it is equal return false otherwise check for next pair.

Complexity:

  • Time Complexity: O(N) N is the length of the string. We are traversing through the string length.
  • Space Complexity: O(1) No extra space required.