22.4.23 [HackerRank]Java Strings Introduction

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

Algorithm

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

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

์ž๋ฐ” ๋ฌธ์ž์—ด ๊ธธ์ด ๊ตฌํ•˜๊ธฐ length()

๋ฌธ์ž์—ด ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๊ณ  ์œ„ํ•ด์„œ๋Š” java.lang.String ํด๋ž˜์Šค์˜ length() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉ.
๋ฌธ์ž์—ด์˜ ๊ธธ์ด (16bit Unicode Character ๊ฐฏ์ˆ˜)๋ฅผ ๋ฆฌํ„ดํ•ด์ค€๋‹ค.

public class StringLength {
 public static void main(String[] args) {
	 String str1 = "ABC";
     String str2 = "A B C";
	 String str3 = "์•ˆ๋…•ํ•˜์„ธ์š”";

     System.out.println(str1.length()); // 3์„ ๋ฆฌํ„ด
     System.out.println(str2.length()); // 5๋ฅผ ๋ฆฌํ„ด
     System.out.println(str3.length()); // 5๋ฅผ ๋ฆฌํ„ด
  }
}

๋ฌธ์ž์—ด ๋น„๊ต ํ•จ์ˆ˜ compareTo()

compareTo() : ๋ฌธ์ž์—ด์˜ ์‚ฌ์ „์ˆœ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ๊ทธ์— ํ•ด๋‹น๋˜๋Š” int๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด A>B ์ผ๋•Œ, A=B์ด๋ฉด 0 A>B์ด๋ฉด 1 A<B์ด๋ฉด -1์„ ๋ฆฌํ„ดํ•œ๋‹ค.

public void test(){
	String str1 = "AA";
	String str2 = "AA":
	String str3 = "BB";
    
	System.out.println(str1.compareto(str2)); // ๊ฒฐ๊ณผ 0
	System.out.println(str2. compareTo(str3)); // ๊ฒฐ๊ณผ -1
	System.out.println(str3.compareTo(str2)): // ๊ฒฐ๊ณผ 1
 }

๋ฌธ์ž์—ด์„ ๋Œ€์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ toLowerCase, toUpperCase

public class String_toCaseTest {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "Hello World";
        
        String substr1 = str.toLowerCase();
        String substr2 = str.toUpperCase();
        
        System.out.println(substr1); // hello world ์ถœ๋ ฅ
        System.out.println(substr2); // HELLO WORLD ์ถœ๋ ฅ
    }
}

๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ substr(a,b) / substring(a,b) / slice(a,b)

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

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

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

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

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        
        Scanner sc=new Scanner(System.in);
        String A=sc.next();
        String B=sc.next();
        /* Enter your code here. Print output to STDOUT. */
        System.out.println(A.length() + B.length());
        // ์ฒซ๋ฒˆ์งธ ์ค„์—๋Š” ๊ฐ A,B์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•ด์„œ ๋”ํ•ด์ค€ ๊ฐ’์„ ํ”„๋ฆฐํŠธ
        if (A.compareTo(B) > 1){
            System.out.println("Yes");
        } else{
            System.out.println("No");
        } // ๋‘๋ฒˆ์งธ ์ค„์—๋Š” A,B ๋น„๊ตํ•ด์„œ A๊ฐ€ B๋ณด๋‹ค ์‚ฌ์ „์ˆœ์œผ๋กœ ๋น ๋ฅด๋ฉด YES ์•„๋‹ˆ๋ฉด No
         System.out.println(A.substring(0,1).toUpperCase()+A.substring(1,A.length())+" "+B.substring(0,1).toUpperCase()+B.substring(1,B.length()));
    } // A๋ฅผ 0๋ฒˆ์งธ๋ถ€ํ„ฐ 1๋ฒˆ์งธ๊นŒ์ง€ ์ž˜๋ผ์„œ ๋Œ€๋ฌธ์ž๋กœ ๋งŒ๋“ค๊ณ , e๋‹ค์‹œ A๋ฅผ ์ฒซ๋ฒˆ์งธ๋ถ€ํ„ฐ A๊ธธ์ด๋งŒํผ ์ž˜๋ผ์„œ ๋ถ™์—ฌ์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ""์œผ๋กœ AB ์‚ฌ์ด์— ๊ณต๋ฐฑ์„ ๋„ฃ์–ด์ค€ ๋‹ค์Œ B๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฒซ๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž์ฒ˜๋ฆฌ ํ•ด์ค€๋‹ค.
}

๐Ÿ‘‰ ์ฐธ๊ณ 

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