22.4.24 [HackerRank]Java Substring

์„œํƒœ์šฑยท2022๋…„ 4์›” 24์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
13/45
post-thumbnail

๐ŸŒฑ ๋ฐฐ๊ฒฝ์ง€์‹

substring(a) : a
a๋ฒˆ์งธ ์ดํ›„๋ถ€ํ„ฐ ํ‘œ์‹œ

"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)

substring(a,b) : a๋Š” ์‹œ์ž‘๊ฐ’, b๋Š” ์ข…๋ฃŒ๊ฐ’
a๋ฒˆ์งธ๋ถ€ํ„ฐ b๋ฒˆ์งธ ์ „๊นŒ์ง€ ์ž˜๋ผ๋‚ด์„œ ํ‘œ์‹œ

 "hamburger".substring(4, 8) returns "urge"
 "smiles".substring(1, 5) returns "mile"

โœ๏ธ ํ•ด์„ค ๋ฐ ๋ถ„์„

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String S = in.next();
        int start = in.nextInt();
        int end = in.nextInt();
        System.out.println(S.substring(start,end));
        // ์ฒซ๋ฒˆ์งธ ์ค„์—๋Š” S๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹จ์ผ ๋ฌธ์ž์—ด, 
        // ๋‘๋ฒˆ์งธ ์ค„์—๋Š” start, end ํฌ์ธํŠธ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜๊ฐ€ Input๋œ๋‹ค.
        // ์‹œ์ž‘์—์„œ ๋-1๊นŒ์ง€ ํฌํ•จํ•˜๋Š” ๋ฒ”์œ„์˜ ๋ฌธ์ž์—ด์„ ํ”„๋ฆฐํŠธํ•ด์•ผ ํ•œ๋‹ค.
        // substring(a,b)๋Š” a๋ฒˆ์งธ๋ถ€ํ„ฐ b๋ฒˆ์งธ ์ „๊นŒ์ง€ ์ž˜๋ผ์„œ ํ”„๋ฆฐํŠธํ•ด์ฃผ๋ฏ€๋กœ ํ™œ์šฉํ•œ๋‹ค.
    }
}

๐Ÿ‘‰ ์ฐธ๊ณ 

0๊ฐœ์˜ ๋Œ“๊ธ€