前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >劳动最光荣!Double Letter Problem from TopCoder

劳动最光荣!Double Letter Problem from TopCoder

作者头像
包子面试培训
发布2018-04-20 16:14:11
6310
发布2018-04-20 16:14:11
举报
文章被收录于专栏:包子铺里聊IT

包子IT面试培训 助你拿到理想的offer!

这是TopCoder上面一道比较简单的250分的题目,非常适合作为面试的第一道热身题目,考察了面试者对于基本的数据结构是否有所了解,如果简单暴力模拟的话,就不是很好。

话不多说,先看一下题目。

Problem Statement

You are given a String S. You can modify this string by repeating the following process:

Find the leftmost occurrence of two consecutive letters in S that are the same.

If you found a pair of identical letters in the first step, delete those two letters from S.

For example, if S="aabccb", you can proceed as follows:

Find and erase "aa", producing the string "bccb".

Find and erase "cc", producing the string "bb".

Find and erase "bb", producing the empty string.

For S="axxyybac" you can do at most two steps, erasing "xx" first and "yy" next. Once you obtain the string "abac", you are done. Note that you cannot erase the two "a"s because they are not consecutive. You want to change S into an empty string by repeating the operation described above. Return "Possible" if you can do that, and "Impossible" otherwise.

Definition

Class:

DoubleLetter

Method:

ableToSolve

Parameters:

String

Returns:

String

Method signature:

String ableToSolve(String S)

(be sure your method is public)

Limits

Time limit (s):

2.000

Memory limit (MB):

256

Constraints

-

S will contain between 1 and 50 characters.

-

Each character in S will be a lowercase English letter ('a'-'z').

Examples

0)

"aabccb"

Returns: "Possible"

1)

"aabccbb"

Returns: "Impossible"

The process will terminate with a single 'b'.

2)

"abcddcba"

Returns: "Possible"

"abcddcba" -> "abccba" -> "abba" -> "aa" -> "".

3)

"abab"

Returns: "Impossible"

No two successive letters are the same, so we can't do any operation.

4)

"aaaaaaaaaa"

Returns: "Possible"

5)

"aababbabbaba"

Returns: "Impossible"

6)

"zzxzxxzxxzzx"

Returns: "Possible"

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2014-09-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 包子铺里聊IT 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 包子IT面试培训 助你拿到理想的offer!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档