字节跳动笔试题一

样例

输入

helllo

wooooooow

AABBCC

输出:

hello

wooc

AABCC

参考代码

package com.itheima.com;

import java.util.Scanner;

public class Dem1 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		 int t ;
	 
		 t = sc.nextInt();
		 String str = null,ans = null;
		 
		 for (int i = 0; i < t; i++) {
				str = sc.next();
				ans = "";
				for(int j = 0 ; j < str.length();j++){
					if(!f1(str.charAt(j),ans) && !f2(str.charAt(j),ans)){
						
						
 /*
 *  // h he hel(判断这步的时候,s[len-1] 和 s[len-2] 还没有相同,所以继续往后面添加字符)
 // hell(到这步的时候,s[len-1] 和 s[len-2] 出现相同了,所以不添加第三个 l 字符,而是添加 o 字符,
  * 所以得到 hello)
 // 出现 s=aab c=b 的情况下不能出现两对
 */
						System.out.println("pre   ans=str[i]="+str.charAt(j));
						ans += str.charAt(j);
						System.out.println("last ans="+ans);
					}
				}
				System.out.println(ans);
		}
		 

	}
    public static 	Boolean f1(char c,String  s) {
    	 
	    int len =  s.length();
	    if( len >=2 && s.charAt(len -1) == c && s.charAt(len-2 )==c) return true;
	    return false;
	}
    public static 	Boolean  f2(char c,String  s) {
	    int len =  s.length();
	    if( len >= 3 && s.charAt(len-1 )  == c && 
	     s.charAt(len-2 ) ==s.charAt(len-3 )   ) 
	    	return true;
	    
	    return false;
	}
}

运行结果示例

3
helllo
pre   ans=str[i]=h
last ans=h
pre   ans=str[i]=e
last ans=he
pre   ans=str[i]=l
last ans=hel
pre   ans=str[i]=l
last ans=hell
pre   ans=str[i]=o
last ans=hello
hello
wooooooow
pre   ans=str[i]=w
last ans=w
pre   ans=str[i]=o
last ans=wo
pre   ans=str[i]=o
last ans=woo
pre   ans=str[i]=w
last ans=woow
woow
AABBCC
pre   ans=str[i]=A
last ans=A
pre   ans=str[i]=A
last ans=AA
pre   ans=str[i]=B
last ans=AAB
pre   ans=str[i]=C
last ans=AABC
pre   ans=str[i]=C
last ans=AABCC
AABCC

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值