492. Maximum Smooth Transitions in a Photo Slideshow
Maximum Smooth Transitions in a Photo Slideshow

You are building a slideshow from the photos on your phone. Each photo is either portrait ('P') or landscape ('L'). The string photos gives their orientations in the order the photos were taken.

A smooth transition happens when the slideshow moves from one photo to the next and both photos have the same orientation. When the orientation changes, the screen has to rotate, so that transition is not smooth.

You may remove any number of photos, including none, but you cannot reorder the photos you keep. The kept photos are shown one after another.

Return the maximum number of smooth transitions the slideshow can have.

Method Signature

int maxSmoothTransitions(String photos)
  • photos is the orientation of each photo, in the order the photos were taken.
  • Returns the maximum number of smooth transitions.

Rules

  • The kept photos may still change orientation. Such a transition is allowed but does not count.
  • A slideshow with only one photo has 0 transitions.

Constraints

  • 1 ≤ photos.length() ≤ 200,000
  • Every character of photos is 'P' or 'L'.

Examples

Example 1

Input: maxSmoothTransitions(photos = "LPLLPL")

Output: 3

Explanation: Remove both portrait photos. The slideshow becomes "LLLL", which has 3 smooth transitions.

Example 2

Input: maxSmoothTransitions(photos = "LPPPLLLL")

Output: 5

Explanation: Keep every photo. "PPP" gives 2 smooth transitions and "LLLL" gives 3, for a total of 5.

Example 3

Input: maxSmoothTransitions(photos = "LLLPLPPPP")

Output: 6

Explanation: Remove the portrait photo at index 3 (0-based). The slideshow becomes "LLLLPPPP", with 3 smooth transitions among the landscape photos and 3 among the portrait photos.

Example 4

Input: maxSmoothTransitions(photos = "PLLPL")

Output: 2

Explanation: Keeping every photo gives only 1 smooth transition. Remove both portrait photos to get "LLL", which has 2 smooth transitions.

Example 5

Input: maxSmoothTransitions(photos = "P")

Output: 0

Explanation: There is only one photo, so there are no transitions.



Please use Laptop/Desktop or any other large screen to add/edit code.