REPOST - this question was answered by someone \'Anonymous\'. Please have it ans
ID: 3883173 • Letter: R
Question
REPOST - this question was answered by someone 'Anonymous'. Please have it answered by an identifiable source. Thank you.
Add a main so that you can put some code there to call your functions and make sure they work: public static void main(String[] args) { /* your testing code goes here */ } Functions you should add if the functionality is possible. Otherwise add an empty function definition with a comment saying "impossible".
Problem #8
public static boolean isPermutationLight(String string1, String string2) {
/* (This functions should return true if string1 is a permutation of string2. In this case you should assume that means contains exactly the same characters, same case, including spaces. The strings may contain ascii or unicode characters. For this question, you should optimize for space and fairly short input strings. */
}
**** Test input: 2 empty Strings; "ab" "ba"; "abc" "acd"; "*rats*" "star**"; "u263A BYE NOW!!" "!BYE NOW! u263A"
Explanation / Answer
package org.students;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
public class CheckPermutatedlyEqual
{
public static void main(String[] args)
{
// Declaring variables
String str1, str2;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
// Getting the input entered by the user
System.out.print("Enter the String#1 :");
str1 = sc.nextLine();
System.out.print("Enter the String#2 :");
str2 = sc.nextLine();
// Calling the method by passing the user entered number
boolean bool = isPermutationLight(str1, str2);
// Displaying the output
if (bool)
{
System.out.println(str1 + " and " + str2 + " are Permutations to each other.");
}
else
{
System.out.println(str1 + " and " + str2 + " are not Permutations to each other.");
}
}
private static boolean isPermutationLight(String str1, String str2)
{
char ch;
if (str1.length() != str2.length())
{
return false;
}
// Creating An HashTable
Hashtable<Character, Integer> h = new Hashtable<Character, Integer>();
/* Getting each character from the string and store in HashTable
* If the same Character appear more than one's in the String
* then its corresponding value will increased every time
*/
for (int i = 0; i < str1.length(); i++)
{
ch = str1.charAt(i);
if (h.containsKey(ch))
{
int value = h.get(ch) + 1;
h.put(ch, value);
}
else
{
h.put(ch, 1);
}
}
/* Getting each character from the string and store in HashTable
* If the same Character appear then the corresponding value will be reduced by 1
*/
for (int i = 0; i < str2.length(); i++)
{
ch = str2.charAt(i);
if (h.containsKey(ch))
{
int value = h.get(ch);
if (value == 0)
{
return false;
}
value = value - 1;
h.put(ch, value);
}
else
{
return false;
}
}
Set keys = h.keySet();
Iterator it = keys.iterator();
// Checking if all the values of corresponding keys are equal to zero or not
while (it.hasNext())
{
ch = (Character)it.next();
if (h.get(ch) != 0)
{
return false;
}
}
return true;
}
}
_________________
Output:
Enter the String#1 :u263A BYE NOW!!
Enter the String#2 :!BYE NOW! u263A
u263A BYE NOW!! and !BYE NOW! u263A are Permutations to each other.
_____________
Output#2:
Enter the String#1 :*rats*
Enter the String#2 :star**
*rats* and star** are Permutations to each other.
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.