1316. Distinct Echo Substrings LeetCode Solution

In this guide, you will get 1316. Distinct Echo Substrings LeetCode Solution with the best time and space complexity. The solution to Distinct Echo Substrings problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Distinct Echo Substrings solution in C++
  4. Distinct Echo Substrings solution in Java
  5. Distinct Echo Substrings solution in Python
  6. Additional Resources
1316. Distinct Echo Substrings LeetCode Solution image

Problem Statement of Distinct Echo Substrings

Return the number of distinct non-empty substrings of text that can be written as the concatenation of some string with itself (i.e. it can be written as a + a where a is some string).

Example 1:

Input: text = “abcabcabc”
Output: 3
Explanation: The 3 substrings are “abcabc”, “bcabca” and “cabcab”.

Example 2:

Input: text = “leetcodeleetcode”
Output: 2
Explanation: The 2 substrings are “ee” and “leetcodeleetcode”.

Constraints:

1 <= text.length <= 2000
text has only lowercase English letters.

Complexity Analysis

  • Time Complexity: O(n^2)
  • Space Complexity: O(n^2)

1316. Distinct Echo Substrings LeetCode Solution in C++

class Solution {
 public:
  int distinctEchoSubstrings(string text) {
    unordered_set<string> seen;

    for (int k = 1; k <= text.length() / 2; ++k) {  // the target length
      int same = 0;
      for (int l = 0, r = k; r < text.length(); ++l, ++r) {
        if (text[l] == text[r])
          ++same;
        else
          same = 0;
        if (same == k) {
          seen.insert(text.substr(l - k + 1, k));
          // Move the window thus leaving a letter behind, so we need to
          // decrease the counter.
          --same;
        }
      }
    }

    return seen.size();
  }
};
/* code provided by PROGIEZ */

1316. Distinct Echo Substrings LeetCode Solution in Java

class Solution {
  public int distinctEchoSubstrings(String text) {
    Set<String> seen = new HashSet<>();

    for (int k = 1; k <= text.length() / 2; ++k) { // the target length
      int same = 0;
      for (int l = 0, r = k; r < text.length(); ++l, ++r) {
        if (text.charAt(l) == text.charAt(r))
          ++same;
        else
          same = 0;
        if (same == k) {
          seen.add(text.substring(l - k + 1, l + 1));
          // Move the window thus leaving a letter behind, so we need to
          // decrease the counter.
          --same;
        }
      }
    }

    return seen.size();
  }
}
// code provided by PROGIEZ

1316. Distinct Echo Substrings LeetCode Solution in Python

class Solution:
  def distinctEchoSubstrings(self, text: str) -> int:
    seen = set()

    for k in range(1, len(text) // 2 + 1):  # the target length
      same = 0
      l = 0
      for r in range(k, len(text)):
        if text[l] == text[r]:
          same += 1
        else:
          same = 0
        if same == k:
          seen.add(text[l - k + 1:l + 1])
          # Move the window thus leaving a letter behind, so we need to
          # decrease the counter.
          same -= 1
        l += 1

    return len(seen)
# code by PROGIEZ

Additional Resources

See also  2242. Maximum Score of a Node Sequence LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.