知识库 知识库
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)

Jim FuckPPT

Java小学生
首页
  • Hyperskill - Java

    • Java basic
    • Java OOP
    • 应知
    • 扩展
    • IO & Stream
    • Error & Exception
    • Algorithm & Data structure
    • Design pattern
    • Web
    • Spring boot
  • 练习题

    • 选择题 & 填空题
    • 代码题
  • Frank - Java与生活 (OOP)

    • 参考资料
    • Java基础
    • OOP上半部分
    • OOP下半部分
  • Frank - Java API进阶

    • Base API
    • Unit Test and main function
  • 学习笔记
  • 学习笔记

    • 数据库
  • Frank - MySQL删库跑路

    • 安装、连接、配置
    • 基本操作——数据库
    • 基本操作——表
    • 基本操作——数据
    • 数据类型
    • 列属性完整性
    • 数据库设计思维
    • 单表查询
    • 多表查询
  • 学习笔记

    • 其它
  • Frank - Linux现代方法

    • 必知
    • 命令
    • 技巧
  • 技术文档
  • Git
  • GitHub技巧
  • 前端
  • Khan Academy - 语法
  • Monthly
  • 阅读
  • Others
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
收藏
  • 标签
  • 归档
GitHub (opens new window)
  • Hyperskill - Java

  • 练习题

    • 选择题 & 填空题

    • 代码题

      • Boxes
      • The longest ascending sequence
      • Right Rotation
      • Book
      • Compact strings with AsciiCharSequence
      • Cinema
      • Calculate square
      • Count words
      • Robot control
      • Concat all strings without digits
      • Find the nearest number
      • Leaderboard
      • Split a list into sublists
      • Arithmetic average
      • Processing strings
        • Topic
        • Problem
        • Hint & Explain
        • Solution
          • My solution
          • Other solution 1
          • Other solution 2
  • Frank - Java与生活

  • Frank - Java API进阶

  • 学习笔记

  • Java
  • 练习题
  • 代码题
Jim
2023-02-18
目录

Processing strings

# Topic

String

# Problem

Number of occurrences

Write a program that finds the frequency of occurrences of a substring in a given string. Substrings cannot overlap: for example, the string ababa contains only one substring aba.

Input data format

The first input line contains a string, the second one contains a substring.

Sample Input 1:

ababa
aba
1
2

Sample Output 1:

1
1

Sample Input 2:

hello there
the
1
2

Sample Output 2:

1
1

Sample Input 3:

hello yellow jello
ll
1
2

Sample Output 3:

3
1

# Hint & Explain

Check out the indexOf(String str, int formIndex) method.

# Solution

# My solution

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // put your code here
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine();
        String subString = scanner.nextLine();
        int fromIndex = 0;
        int occurIndex;
        int counter = 0;

        do {
            if (fromIndex > string.length() - 1) {
                break;
            }
            occurIndex = string.indexOf(subString, fromIndex);
            if (occurIndex != -1) {
                counter++;
                fromIndex = occurIndex + subString.length();
            }
        } while (occurIndex != -1);

        System.out.println(counter);

    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# Other solution 1

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // put your code here
        Scanner scanner = new Scanner(System.in);
        String sentence = " " + scanner.nextLine() + " ";
        String query = scanner.nextLine().trim();
        System.out.print(sentence.split(query).length - 1);
    }
}
1
2
3
4
5
6
7
8
9
10
11

# Other solution 2

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String string = scanner.nextLine();
        String substring = scanner.nextLine();
        System.out.println((string.length() - string.replace(substring, "").length()) / substring.length());
    }
}
1
2
3
4
5
6
7
8
9
10
编辑 (opens new window)
上次更新: 2023/04/14, 12:58:00
Arithmetic average
参考资料

← Arithmetic average 参考资料→

最近更新
01
《挪威的森林》
04-14
02
青钢影
04-14
03
Arithmetic average
12-31
更多文章>
Theme by Vdoing | Copyright © 2022-2023 Jim Frank | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式