Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Permutation {
- boolean permutation(String s, String t) {
- if (s.length() != t.length()) {
- return false;
- }
- //memo 저장 공간
- int[] marking = new int[128];
- //첫 번째 문자열을 memo +1
- char[] s_array = s.toCharArray();
- for (char c : s_array) {
- marking[c]++;
- }
- //이미 memo된 문자 요소와 두 번째 문자 요소 memo -1
- for (int i = 0; i < t.length(); i++) {
- int c = t.charAt(i);
- marking[c]--;
- //음수 발생 시, 두 문자열은 같이 않음
- if (marking[c] < 0) {
- return false;
- }
- }
- return true;
- }
- public static void main(String[] args) {
- Permutation p = new Permutation();
- System.out.println(p.permutation("abc", "bca"));
- }
- }
Add Comment
Please, Sign In to add comment