Reverse Words in a String

How to reverse words in a String?

In this problem, we have given an input string, we have to write a code to reverse the string word by word.

Example 1:

Input : “the carpet is green”

Output: “green is carpet the”

Example 2:

Input : ”  Java Ebook  “

Output: “Ebook Java”

Explanation: The reversed string should not contain leading or trailing spaces.

Example 3:

Input : “a good   example”

Output: “example good a”

Explanation: We have reduce multiple spaces between two words to a single space in the reversed string.

Note:

  • The Input string may contain leading or trailing spaces. However, the reversed string should not contain leading or trailing spaces.
  • We have to reduce multiple spaces between two words to a single space in the reversed string.

Check If a String is Subsequence of Another String

In this tutorial, I am going to discuss a problem Is Subsequence (check if a string is subsequence of another string).

Given two strings str1 and str2, check if str1 is a subsequence of str2. Both strings consists only of lowercase characters.

Example 1:

Input: str1 = “abc”, str2 = “ahbgdc”

Output: true

Example 2:

Input: s = “axc”, t = “ahbgdc”

Output: false

Backspace String Compare

Let’s discuss an interesting problem Backspace String Compare.

Given two strings S and T containing backspaces. We have to write a code to check if they are equal.  In string, # means a backspace character.

Example 1:

Input: S = “ab#c”, T = “ad#c”

Output: true

Explanation: Both S and T become “ac”.

Example 2:

Input: S = “ab##”, T = “c#d#”

Output: true

Explanation: Both S and T become “”.

Example 3:

Input: S = “a##c”, T = “#a#c”

Output: true

Explanation: Both S and T become “c”.

Example 4:

Input: S = “a#c”, T = “b”

Output: false

Explanation: S becomes “c” while T becomes “b”.