Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8434461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:39:45+00:00 2026-06-10T06:39:45+00:00

this question might seem basic to some people but I’ve been analysing and dissecting

  • 0

this question might seem basic to some people but I’ve been analysing and dissecting this code without success as to how this permutation program by Robert Sedgewick prints the combination o f words or characters without using system.out.print in the methods perm1 and perm2. Any help or explanation for dummies is greatly appreciated. Thank you in advance.

This is the code under the link:

public class Permutations {
  // print N! permutation of the characters of the string s (in order)
  public  static void perm1(String s) { perm1("", s); }
  private static void perm1(String prefix, String s) {
      int N = s.length();
      if (N == 0) System.out.println(prefix);
      else {
          for (int i = 0; i < N; i++)
             perm1(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1, N));
      }
  }
  // print N! permutation of the elements of array a (not in order)
  public static void perm2(String s) {
     int N = s.length();
     char[] a = new char[N];
     for (int i = 0; i < N; i++)
         a[i] = s.charAt(i);
     perm2(a, N);
  }

  private static void perm2(char[] a, int n) {
      if (n == 1) {
          System.out.println(a);
          return;
      }
      for (int i = 0; i < n; i++) {
          swap(a, i, n-1);
          perm2(a, n-1);
          swap(a, i, n-1);
      }
  }  

  // swap the characters at indices i and j
  private static void swap(char[] a, int i, int j) {
      char c;
      c = a[i]; a[i] = a[j]; a[j] = c;
  }

  public static void main(String[] args) {
     int N = Integer.parseInt(args[0]);
     String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
     String elements = alphabet.substring(0, N);
     perm1(elements);
     System.out.println();
     perm2(elements);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:39:47+00:00Added an answer on June 10, 2026 at 6:39 am

    It’s right there:

    if (N == 0) System.out.println(prefix);
    

    System.out.print and System.out.println is basically the same, except the later prints a newline after the text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This might be a basic question, but I can't seem to find an answer.
This might seem an easy question for some, but I am struggling with it.
This might seem like a very easy question for some of you folks, but
This question might seem very specific but I am in need of some ideas
This might seem like a silly question, but after asking some questions on stackoverflow
I know this question might seem a little basic stuff but I want to
This might seem like a basic question and back to Http protocol 101. But
This might seem like a really basic question but, When dividing the output of
This question might seem strange but I have been searching for an answer for
This question might not seem very technical but, out of curiosity,I want to know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.