Bランクレベルアップメニュー 文字列の重複カウント Java 解答

# STEP: 30 文字列の長さ

## 問題
https://paiza.jp/works/mondai/prob60/count_string_0

## 解答
```java:step.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        
        // 文字数を取得
        int length = line.length();
        System.out.println(length);
        
        sc.close();
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/60bb07f2-d435-8683-37f8-94275253540b.png)


# STEP: 31 文字列の1文字目
## 問題
https://paiza.jp/works/mondai/prob60/count_string_1

## 解答
```java:step31.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        displayFirstCharacter(sc);
        sc.close();
    }
    
    /**
     * 1文字目を表示する関数
     * 
     * @param sc 標準入力
     * */
    public static void displayFirstCharacter(Scanner sc){
        
        // 文字目を区切る
        sc.useDelimiter("");
        String firstLetter = sc.next();
        System.out.println(firstLetter);
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/369feb92-dcb4-1c4f-b8d8-47cfa533b71c.png)


# STEP: 32 １文字ずつ出力
## 問題
https://paiza.jp/works/mondai/prob60/count_string_2

## 解答
```java:step32.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        outputOneCharacterAtTime(sc);
        sc.close();
    }
    
    /**
     * 1文字ずつ出力する関数
     * @ param sc 標準入力
     * */
    public static void outputOneCharacterAtTime(Scanner sc){
        // ""で文字を区切る
        sc.useDelimiter("");
        while(sc.hasNext()){
            System.out.println(sc.next());
        }
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/6a16d252-b24f-fd46-fca4-f9f3bbecb8a3.png)


# STEP: 33 文字列の１、２文字目
## 問題
https://paiza.jp/works/mondai/prob60/count_string_3

## 解答
```java:step33.java
import java.util.*;


public class Main {
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
        // 半角スペースで区切る
        sc.useDelimiter("");
        String firstLetter = sc.next();
        String secondLetter = sc.next();

        System.out.println(firstLetter + " " + secondLetter);
        sc.close();
    }
    

}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/cca5b168-eeda-0897-20cc-29f1d277e080.png)


# STEP: 34 文字列の n 文字目と n + 1 文字目

## 問題
https://paiza.jp/works/mondai/prob60/count_string_4

## 解答
```java:step34.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        
        // n 文字目を指定する変数(配列は0から始まる為)
        int n = sc.nextInt();

        String line = sc.next();
        String answer = outputNthCcharacterPlusNPlus1stCharacter(n, line);
        System.out.println(answer);
       // test();
        sc.close();
    }
    
    /**
     * n 文字目と n 足す1 文字目出力する関数
     * 
     * @param n 開始文字
     * @param line 対象文字列
     * 
     * @return 結果
     * */
    public static String outputNthCcharacterPlusNPlus1stCharacter(int n, String line){
     
        String answer = "";
        if(line.length() > 1 && n < line.length()){
            String part = line.substring(n-1, n+1);
            String[] split = part.split("");
            answer = split[0] + " " + split[1];
        }
        
        return answer;
    }
    
    private static List<List<String>> testLists = Arrays.asList(
            Arrays.asList("1","abcd","a b"),
            Arrays.asList("3","abcd","c d"),
            Arrays.asList("4","abcd",""),
            Arrays.asList("10","abcd","")
        );
        
    private static void test(){
        

        for(List<String> testList : testLists){
            int n = Integer.valueOf(testList.get(0));
            String line = testList.get(1);
            String answer = outputNthCcharacterPlusNPlus1stCharacter(n, line);
            if(Objects.equals(answer, testList.get(2))){
                 System.out.println("OK:" + answer);
            } else {
                 System.out.println("NG:" + answer);
            }
        }
    }
}
```
## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/4c2a68d9-8858-613a-e54f-529bbca53660.png)


# FINAL問題 文字列の重複カウント
## 問題
https://paiza.jp/works/mondai/prob60/count_string_9

## 解答
```java:final.java
import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String t = sc.nextLine();
        int answer = getMatchCount(s, t);
        System.out.println(answer);
        //test();
        sc.close();
    }
    
    /**
     * 一致する文字の数を数える関数
     * 
     * @param s 検索文字列
     * @param t 検索対象文字列
     * 
     * @return 結果
     * */
    public static int getMatchCount(String s, String t){
        String searchString = t;
        int count = 0;
        int index = -1;
        do{

            // 一致するインデックスを返却
            index = searchString.indexOf(s);
            if(index != -1){
                count++;
                // 1文字ずつ
                searchString = searchString.substring(index + 1);
            }

        }while(index != -1); 
        
        return count;
        
    }

    private static List<List<String>> testLists = Arrays.asList(
        Arrays.asList("AA","abdeeAAbAAAbfde", "3"),
        Arrays.asList("el","scale", "0"),
        Arrays.asList("Ji","JiJiiJiIjiIJjIJi", "4")
        );
        
    private static void test(){
        for(List<String> testList : testLists){
            String s = testList.get(0);
            String t = testList.get(1);
            int answer = getMatchCount(s, t);
            if(answer == Integer.valueOf(testList.get(2))){
                System.out.println("OK:" + answer);
            }else{
                System.out.println("NG:" + answer);               
            }
        }
    }
}
```

## 結果

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2918186/53aa8bc0-03a8-1786-efbd-56094a2314f6.png)