Seize your moment! ๐Ÿ‘พ

์•ˆ๋…•ํ•˜์„ธ์š”. Eric์ž…๋‹ˆ๋‹ค. ์ œ ๋ธ”๋กœ๊ทธ์— ๋ฐฉ๋ฌธํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ป ๊ฐœ๋ฐœ๊ณต๋ถ€/Algorithm

[Eric's ๋ฐฑ์ค€] 10809๋ฒˆ - ์•ŒํŒŒ๋ฒณ์ฐพ๊ธฐ - Java

Eric_ko 2023. 1. 26. 21:53

์•ˆ๋…•ํ•˜์„ธ์š”! Eric ์ž…๋‹ˆ๋‹ค.

์˜ค๋Š˜์€ ์•„์ฃผ ์‰ฌ์šด ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์˜ค๋Š˜์˜ ๋ฌธ์ œ๋Š” ์•ŒํŒŒ๋ฒณ ์ฐพ๊ธฐ ๋ฌธ์ œ๋กœ์จ

java์˜ indexOf ๋ฅผ ์ด์šฉํ•ด์„œ ์ €๋Š” ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ €๋Š” ์ž…๋ ฅ๋˜๋Š” ๊ฐ’์˜ string ๊ฐ’์— indexOf ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ์ด ์žˆ๋‹ค๋ฉด ๊ทธ ๋ฌธ์ž์˜ ์œ„์น˜๊ฐ€ ๋‚˜์˜ฌ๊ฒƒ์ด๊ณ ,

ํ•ด๋‹น  ์•ŒํŒŒ๋ฒณ์ด ์—†๋‹ค๋ฉด ์ž๋™์œผ๋กœ indexOf ์˜ ๊ฐ’์ด -1 ์ด ๋‚˜์˜ฌ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ €๋Š” for๋ฌธ์—์„œ i=0 ๋ถ€ํ„ฐ ๋Œ์ง€๋งŒ, ASCII code๋ฅผ ์ด์šฉํ•˜๊ธฐ์œ„ํ•ด์„œ

i +97 ์„ํ•˜๊ณ  ์ด๋ฅผ char ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋ชจ๋‘ ์†Œ๋ฌธ์ž ์•ŒํŒŒ๋ฒณ์ด ๋‚˜์˜ค๊ธฐ๋กœ ํ•˜์˜€์œผ๋ฏ€๋กœ

ASCII ์ฝ”๋“œ์— ๋งž๋Š” ๊ฐ’์ด ๋‚˜์˜ค๋ฉด indexOf ๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ”๋กœ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

import java.util.Scanner;
public class N10809 {
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        String str = kb.next();
        for (int i = 0; i < 26; i++) {
            char tmp = (char) (i + 97);
            System.out.print(str.indexOf(tmp) + " ");
        }
    }
}