✔ 내 답-> 정답
import java.util.*;
public class Main{
public static int solution(String S, String T){
int n =T.length();
char[] arrS=S.toCharArray();
int answer=0;
HashMap<Character,Integer> mapT= new HashMap<>();
for(char x: T.toCharArray()){
mapT.put(x,mapT.getOrDefault(x,0)+1);
}
for(int i=0;i<=S.length()-n;i++){
HashMap<Character,Integer> mapS= new HashMap<>();
for(int j=i;j<=i+n-1;j++){
mapS.put(arrS[j],mapS.getOrDefault(arrS[j],0)+1);
}
//Set set = mapS.entrySet();
//System.out.println(set);
if(mapT.equals(mapS)) answer++;
}
return answer;
}
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
String S=kb.next();
String T = kb.next();
System.out.println( solution(S,T));
}
}
✔️ 강의 50%로 보고 구현-> 100%듣고 난 후랑 같음 ->정답
import java.util.*;
public class Main {
public static int solution(String s, String t){
int answer=0;
int tlen = t.length();
HashMap <Character,Integer> mapS = new HashMap<>();
HashMap <Character,Integer> mapT = new HashMap<>();
for(char x: t.toCharArray()) {
mapT.put(x, mapT.getOrDefault(x,0)+1);
}
for(int i=0;i<tlen-1;i++) {
mapS.put(s.charAt(i), mapS.getOrDefault(s.charAt(i),0)+1);
}
int lt =0;
for(int rt =tlen-1;rt<s.length();rt++) {
mapS.put(s.charAt(rt), mapS.getOrDefault(s.charAt(rt), 0)+1);
if(mapS.equals(mapT)) answer++;
mapS.put(s.charAt(lt), mapS.getOrDefault(s.charAt(lt), 0)-1);
if(mapS.get(s.charAt(lt))==0) mapS.remove(s.charAt(lt));
lt++;
}
return answer;
}
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
String s = kb.next();
String t = kb.next();
System.out.println(solution(s,t));
}
}