Skip to content

stringPermutations function has exponential time complexity causing severe performance degradation #138

Description

@Shree-Gowda

Description:
The current stringPermutations function uses a recursive approach with exponential time complexity O(n!). For a 10-character string, it takes ~2 seconds to complete, which is unacceptable for production use.

Current Implementation Problems:

  1. Exponential Time Complexity: O(n!) - For n=10, that's 3,628,800 operations
  2. Memory Inefficiency: Uses Set to store all permutations in memory
  3. No Early Termination: Cannot limit the number of permutations generated
  4. Recursive Stack Overflow Risk: Deep recursion for large strings

Performance Impact:

  • 10 characters: ~2 seconds
  • 11 characters: ~22 seconds (estimated)
  • 12 characters: ~4+ minutes (estimated)

Proposed Solutions:

  1. Iterative Implementation: Replace recursion with iteration
  2. Generator Pattern: Return permutations on-demand instead of all at once
  3. Early Termination: Add optional limit parameter
  4. Memory Optimization: Use more efficient data structures

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions