194k views
1 vote
In this programming assignment, you will design in pseudo code and implement in Java two versions of a program that deals with two strings, referred to as shortStr and longStr. The program attempts to find if any/all possible permutation of shortStr exists within longStr, and if so, at which location on longStr.

1 Answer

4 votes

Answer:

// Program is written in Java Programming Language

// Comments are used for explanatory purpose

import java.util.*;

public class Perm {

/* function to check whether a strings is a permutation of the other */

static boolean arePermutation(String shortStr, String longStr)

{

// Get lenghts of both strings

int n1 = shortStr.length();

int n2 = longStr.length();

int pos; // Permutation Position

char ch1[] = shortStr.toCharArray();

char ch2[] = longStr.toCharArray();

// Sort both strings

Arrays.sort(ch1);

Arrays.sort(ch2);

// Compare sorted strings

for (int i = 0; i < n1; i++)

if (ch1[i] != ch2[i])

return false;

else

pos = i;

return true;

}

// Program Main Method

public static void main(String[] args)

{

String shortStr, longStr; int pos;

Scanner input = new Scanner(System.in);

System.out.print("Enter any two strings");

shortStr = input.nextLine();

longStr = input.nextLine();

if (arePermutation(shortStr, longStr)

System.out.println("Yes");

else

System.out.println("No");

}

}

User PfMusk
by
5.1k points