01-24 13:36
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_33 ๋ชจ๋“  ์•„๋‚˜๊ทธ๋žจ ์ฐพ๊ธฐ ๋ณธ๋ฌธ

Algorithm/Java

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_33 ๋ชจ๋“  ์•„๋‚˜๊ทธ๋žจ ์ฐพ๊ธฐ

miinsun 2022. 1. 5. 19:44

 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…

S๋ฌธ์ž์—ด์—์„œ T๋ฌธ์ž์—ด๊ณผ ์•„๋‚˜๊ทธ๋žจ์ด ๋˜๋Š” S์˜ ๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.
์•„๋‚˜๊ทธ๋žจ ํŒ๋ณ„์‹œ ๋Œ€์†Œ๋ฌธ์ž๊ฐ€ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค.
๋ถ€๋ถ„๋ฌธ์ž์—ด์€ ์—ฐ์†๋œ ๋ฌธ์ž์—ด์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿ”จ ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ - ์ฒซ ์ค„์— ์ฒซ ๋ฒˆ์งธ S๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ค„์— T๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค.

S๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 10,000์„ ๋„˜์ง€ ์•Š์œผ๋ฉฐ, T๋ฌธ์ž์—ด์€ S๋ฌธ์ž์—ด๋ณด๋‹ค ๊ธธ์ด๊ฐ€ ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์Šต๋‹ˆ๋‹ค.

bacaAacba
abc

 

์ถœ๋ ฅ - S๋‹จ์–ด์— T๋ฌธ์ž์—ด๊ณผ ์•„๋‚˜๊ทธ๋žจ์ด ๋˜๋Š” ๋ถ€๋ถ„๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

3

 

โ€‹

๐Ÿ’ป Solution.java

import java.util.*;

public class Main {

	public int solution(String a, String b) {
		int answer = 0;
		HashMap<Character, Integer> am = new HashMap<>();
		HashMap<Character, Integer> bm = new HashMap<>();
		for(char x: b.toCharArray()) bm.put(x, bm.getOrDefault(x, 0) + 1);
		
		int L = b.length() - 1;
		for(int i = 0; i < L; i++) am.put(a.charAt(i), am.getOrDefault(a.charAt(i), 0) + 1);
		int lt =0;
		for(int rt = L; rt < a.length(); rt++) {
			am.put(a.charAt(rt), am.getOrDefault(a.charAt(rt), 0)  + 1);
			if(am.equals(bm)) answer++;
			am.put(a.charAt(lt), am.get(a.charAt(lt)) - 1);
			if(am.get(a.charAt(lt)) == 0) am.remove(a.charAt(lt));
			lt++;
		}
        return answer;
	}
	
	public static void main(String[] args){
		Main main  = new Main();
		Scanner sc =new Scanner(System.in);
		
		String s = sc.next();
		String t = sc.next();
		
		System.out.println(main.solution(s, t));
		
		sc.close();
		return ;
	}
}

 

 

 

Comments