HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMajor

Determining whether two scrambled strings are equivalent

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
determiningequivalentarestringstwoscrambledwhether

Problem

I was able to complete the problem doing it in this manner, but I would assume there has to be an easier way.

Sample input:


abc, cba

Output:


Both strings of characters are equivalent.

Or


abc, acc

Output:


Different

```
import java.util.Scanner;

public class StringTest {

public static void main(String[] args) {
String str1, str2;
int aCount = 0, bCount = 0, cCount = 0, dCount = 0, eCount = 0, fCount = 0, gCount = 0, hCount = 0, iCount = 0, jCount = 0, kCount = 0, lCount = 0, mCount = 0, nCount = 0, oCount = 0, pCount = 0, qCount = 0, rCount = 0, sCount = 0, tCount = 0, uCount = 0, vCount = 0, wCount = 0, xCount = 0, yCount = 0, zCount = 0;
int aCount2 = 0, bCount2 = 0, cCount2 = 0, dCount2 = 0, eCount2 = 0, fCount2 = 0, gCount2 = 0, hCount2 = 0, iCount2 = 0, jCount2 = 0, kCount2 = 0, lCount2 = 0, mCount2 = 0, nCount2 = 0, oCount2 = 0, pCount2 = 0, qCount2 = 0, rCount2 = 0, sCount2 = 0, tCount2 = 0, uCount2 = 0, vCount2 = 0, wCount2 = 0, xCount2 = 0, yCount2 = 0, zCount2 = 0;
Scanner input = new Scanner(System.in);

System.out
.println("Enter your first string that you want compared (lowercase only):");

str1 = input.next();
str1 = str1.trim();
char[] charArr1 = str1.toCharArray();

System.out
.println("Enter your second string that you want compared (lowercase only):");

str2 = input.next();
str2 = str2.trim();
char[] charArr2 = str2.toCharArray();

input.close();

for (int i = 0; i < charArr1.length; i++) {
if (charArr1[i] == 'a')
aCount++;
if (charArr1[i] == 'b')
bCount++;
if (charArr1[i] == 'c')
cCount++;
if (charArr1[i] == 'd')
dCount++;
if (charArr1[i] == 'e')
eCount++;
if (charArr1[i] == 'f')
fCount++;
if (charArr1[i]

Solution

This is where the Arrays utility class comes in handy. You can use it to sort the character arrays underlying both strings and then compare the sorted character arrays:

public static boolean equivalent(String s1, String s2) {
    char[] chars1 = s1.toCharArray();
    char[] chars2 = s2.toCharArray();

    Arrays.sort(chars1);
    Arrays.sort(chars2);

    return Arrays.equals(chars1, chars2);
}


This method yields the expected results:

assert equivalent("abc", "cba");
assert !equivalent("abc", "acc");


If there's a possibility for one or both of the input strings to be null, you could add the following two checks to the beginning of the method:

if (s1 == null && s2 == null) return true;
if (s1 == null || s2 == null) return false;


If performance is important, you could also add a string length comparison to avoid unnecessary array sorting:

if (s1.length() != s2.length()) return false;

Code Snippets

public static boolean equivalent(String s1, String s2) {
    char[] chars1 = s1.toCharArray();
    char[] chars2 = s2.toCharArray();

    Arrays.sort(chars1);
    Arrays.sort(chars2);

    return Arrays.equals(chars1, chars2);
}
assert equivalent("abc", "cba");
assert !equivalent("abc", "acc");
if (s1 == null && s2 == null) return true;
if (s1 == null || s2 == null) return false;
if (s1.length() != s2.length()) return false;

Context

StackExchange Code Review Q#70361, answer score: 27

Revisions (0)

No revisions yet.