Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

String Permutations Editorial

DSA Editorial, Solution and Code

Practice Problem Link: https://workat.tech/problem-solving/practice/string-permutations

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a string s with distinct lowercase English characters, find all the permutations of s.

Approach

In this problem both the naive and the optimal approaches are equivalent. We can sort the given string lexicographically and try to generate its next greater permutation, till no other permutation remains.

Analysis

  • Time Complexity: O(n * n!)
  • Space Complexity: O(n * n!)

Implementation

C++
vector<string> result;
void permute(string s, string possible) {
	if(s.size() == 0) {
		result.push_back(possible);
		return;
	}
	for(int i = 0; i < s.size(); i++) {
		string left = s.substr(0, i);
		string right = s.substr(i + 1, s.size() - i - 1);
		string total = left + right;
		permute(total, possible + s[i]);
	}
}
vector<string> getAllPermutations(string s) {
	result.clear();
	sort(s.begin(), s.end());
	permute(s, "");
	return result;
}
Java
class Solution {
	static List<String> getAllPermutations(String s) {
		ArrayList<String> arr = new ArrayList<>();
		permute(s, 0, s.length() - 1, arr);
		return arr;
	}
	public static void permute(String s, int left, int right,ArrayList<String> arr) {
        if (left == right) {
            arr.add(s);
		}
        else {
            for (int i = left; i <= right; i++) {
                s = swap(s, left, i);
                permute(s, left+1, right,arr);
                s = swap(s, left, i);
            }
        }
    }
    public static String swap(String a, int i, int j) {
        char temp;
        char[] charArray = a.toCharArray();
        temp = charArray[i] ;
        charArray[i] = charArray[j];
        charArray[j] = temp;
        return String.valueOf(charArray);
    }
}
Related Content
Combination Sum 1
Combination Sum 2
Combinations
Consecutively Descending Integers
Generate Parentheses
Kth Permutation Sequence
Letter Combinations of a Phone Number
N Queens Problem
Palindrome Partitioning
Rat In A Maze
Restore IP Addresses
Subset Sum
Subsets
Subsets - II
Sudoku Solver
Tug of War
Word Break
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet