P1234 小A的口头禅 java题解

题目:不过多叙述,直接上思路🤭,相信来看题解的都是已经看过题得了。

        题目说的“hehe”字符串不考虑方向,所以“eheh”也要判断,还需要合并每一列的字符在判断”hehe“和”eheh“在字符串里出现了多少次。(思路简单直接上代码)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int count = 0;//统计数量
        char ch[][]=new char[n][m];
        String str[]=new String[n+m];//一共n+m个字符串
        for (int i = 0; i < n; i++) {
        	str[i]=sc.next();
        	ch[i]=str[i].toCharArray();
        }
//        for(int i=0;i<n;i++) {
//        	for(int j=0;j<m;j++) {
//        		System.out.print(ch[i][j]);
//        	}
//        	System.out.println();
//        }
        for(int i=n;i<n+m;i++) {//统计每一列的字符组成的字符串
        	str[i]="";
        	for(int j=0;j<n;j++) {
        		str[i]+=ch[j][i-n];
        	}
//        	System.out.println(str[i]);
        }
        for(int i=0;i<n+m;i++) {
        	for(int j=0,k=4;k<=str[i].length();j++,k++) {
        		String temp=str[i].substring(j,k);
        		if("hehe".equals(temp)) {
        			count++;
        		}else if("eheh".equals(temp)) {
        			count++;
        		}
        	}
        }
        System.out.println(count);
    }
}

各位,不懂可以私信但是相信各位都是大佬,🤭

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值