오늘의 취준/오늘의 코테

[JAVA] 알고리즘 문제풀이 입문 1.01번

gogoem 2023. 4. 28. 11:55
728x90

강의: https://inf.run/w779

 

자바(Java) 알고리즘 문제풀이 입문: 코딩테스트 대비 - 인프런 | 강의

자바(Java)로 코딩테스트를 준비하시는 분을 위한 강좌입니다. 코딩테스트에서 가장 많이 출제되는 Top 10 Topic을 다루고 있습니다. 주제와 연동하여 기초문제부터 중급문제까지 단계적으로 구성

www.inflearn.com

 

 

import java.util.Scanner;

class word{
    String sentence, word;

    public word(String sentence, String word){
        this.sentence = sentence;
        this.word = word;
    }
}


public class Fword{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        String sentence = scanner.nextLine();
        String word = scanner.nextLine();
        scanner.close();

        word fWord = new word(sentence.toLowerCase(), word.toLowerCase());
        int length = fWord.sentence.length() - fWord.sentence.replace(word.toLowerCase(), "").length();
        int res = length/word.length();
        System.out.print(res);
    }
}
  • scanner.next() : 스페이스로 구분해서 입력 / scanner.nextLine() : 엔터로 구분해서 입력
  • int length = fWord.sentence.length() - fWord.sentence.replace(word.toLowerCase(), "").length();

            ㄴ전체 문장 길이 - 원하는 문자를 공백으로 치환한(원하는 문자가 빠진) 문장 길이

  • nt res = length/word.length();

           구해야하는것은 특정 단어가 몇 번 들어갔느냐 이므로 위의 결과값을 원하는 단어의 길이로 나눠줌.

 

 

  • 클래스와 객체가 딱히 의미가 없는 것 같아 삭제한 코드.
import java.util.Scanner;

public class Fword{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        String sentence = scanner.nextLine().toLowerCase();
        String word = scanner.nextLine().toLowerCase();
        scanner.close();

        int length = sentence.length() - sentence.replace(word.toLowerCase(), "").length();
        int res = length/word.length();
        System.out.print(res);
    }
}

 

 

 

청년취업준비쿠폰지원사업 내역 증빙 - 고은미