Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Question 1_2 #155

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions java/Chapter 1/Question1_2/Question1_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package question1_2;

/**
*
* @author albertovega
*/
public class Question1_2 {

public static String sort(String s) {
char[] content = s.toCharArray();
java.util.Arrays.sort(content);
return new String(content);
}

public static boolean permutation(String s, String t) {
if (s.length() != t.length()) {
return false;
}
return sort(s).equals(sort(t));
}
public static boolean permutation2(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] letters = new int[128]; // Asumption

char[] s_array = s.toCharArray();
for (char c : s_array) { // count number of each char in s.
letters[c]++;
}

for (int i = 0; i < t.length(); i++) {
int c = (int) t.charAt(i);
letters[c]--;
if(letters[c] < 0) {
return false;
}
}
return true;
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String string1 = "dog";
String string2 = "god";

boolean result;
result = permutation(string1, string2);
System.out.println(result);
boolean result2;
result2 = permutation2(string1, string2);
System.out.println(result2);


}
}