Skip to content

Instantly share code, notes, and snippets.

@larry-liu
Created June 18, 2014 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larry-liu/c3ec4c9f7f5586864963 to your computer and use it in GitHub Desktop.
Save larry-liu/c3ec4c9f7f5586864963 to your computer and use it in GitHub Desktop.
CC1.3
import java.io.*;
import java.util.*;
class permutation {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "cba";
if(permutation(str1, str2))
System.out.println("They are each other's permutation");
else
System.out.println("They are not each other's permutation");
}
public static boolean permutation(String str1, String str2) {
int[] count = new int[256];
int len1 = str1.length();
int len2 = str2.length();
if(len1 != len2)
return false
boolean success =true;
for(int i = 0; i < len1; i++) {
count[str1.charAt(i)] ++;
}
for(int i = 0; i < len2; i ++) {
count[str1.charAt(i)] --;
}
for(int i = 0; i < 256; i ++) {
if(count[i] != 0)
success = false;
}
return success;
}
}
@diegozeng
Copy link

A small mistake at line 25, “str1.charAt(i)” should be "str2.charAt(i)", otherwise it always returns "true" when “len1 == len2”.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment