bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1055. Shortest Way to Form String

1055. Shortest Way to Form String

Leetcode Solutions

Approach: Two Pointers

  1. Check if all characters of target are present in source. If not, return -1.
  2. Initialize two pointers: sourcePointer to 0 and targetPointer to 0.
  3. Initialize a counter count to 1.
  4. While targetPointer is less than the length of target: a. If sourcePointer is equal to the length of source, reset it to 0 and increment count. b. If the character at sourcePointer matches the character at targetPointer, increment both pointers. c. Otherwise, increment only sourcePointer.
  5. Once all characters in target are matched, return count.
UML Thumbnail

Approach:D Array

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...