문제: https://www.hackerrank.com/challenges/anagram/problem
import sys
read = lambda : sys.stdin.readline().strip()
for _ in range(int(read())):
str = read()
length = len(str)
count = 0
if length % 2 != 0:
print(-1)
continue
s1 = str[0:length//2]
s2 = str[length//2: length]
for index, c in enumerate(s1):
try:
index_of = s2.index(c)
# s1에 있는 글자가 s2에 있고, 그것의 위치가 있다.
# print('before s2:', s2,'s1:', c)
s2 = s2[0:index_of] + s2[index_of+1:]
# print('after s2:', s2)
except:
# 못찾으면 바꿔야 하니까 count를 올린다.
count += 1
print(count)
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-->0){
String str = sc.next();
int len = str.length(), count = 0;
if (len%2==1){
System.out.println(-1);
continue;
}
String s1 = str.substring(0,len/2);
String s2 = str.substring(len/2,len);
char[] s1Chars = s1.toCharArray();
for (char c : s1Chars){
int index = s2.indexOf(c);
if (index == -1){
count++;
} else {
s2 = s2.substring(0,index)+s2.substring(index+1);
}
}
System.out.println(count);
}
}
}
java는 https://www.hackerrank.com/challenges/anagram/forum 에서 tao_zhang 풀이를 참고했습니다