DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Find All Permutations of string - C#

Find All Permutations of string - C#

Aniruddha Deshpande user avatar by
Aniruddha Deshpande
·
Jun. 02, 12 · Web Dev Zone · Code Snippet
Like (0)
Save
Tweet
16.19K Views

Join the DZone community and get the full member experience.

Join For Free

Find All Permutations of string - C# 

// 1. remove first char

// 2. find permutations of the rest of chars

// 3. Attach the first char to each of those permutations.

// 3.1 for each permutation, move firstChar in all indexes to produce even more permutations.

// 4. Return list of possible permutations.


public string[] FindPermutations(string word)

{

if (word.Length == 2)

{

char[] _c = word.ToCharArray();

string s = new string(new char[] { _c[1], _c[0] });

return new string[]

{

word,

s

};

}


List _result = new List();


string[] _subsetPermutations = FindPermutations(word.Substring(1));

char _firstChar = word[0];

foreach (string s in _subsetPermutations)

{

string _temp = _firstChar.ToString() + s;

_result.Add(_temp);

char[] _chars = _temp.ToCharArray();

for (int i = 0; i

Strings Data Types

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Ultra-Fast Microservices: When Microstream Meets Payara
  • Debugging Deadlocks and Race Conditions
  • How to Generate Fake Test Data
  • Java: Why Core-to-Core Latency Matters

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo