检查字符串中连续的重复字符 [英] Check a string for consecutive repeated characters

查看:231
本文介绍了检查字符串中连续的重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

面试时要求用Java编写代码以显示不具有连续重复字符的字符串。

It is asked in an interview to write the code in Java to display the string which doesn't have consecutive repeated characters.

例如:Google,Apple,Amazon ;
应该显示 Amazon

E.g.: Google, Apple, Amazon; It should display "Amazon"

我编写了代码以查找继续重复的char。有什么算法或有效的方法可以找到它吗?

I wrote code to find continues repeating char. Is there any algorithm or efficient way to find it?

推荐答案

class replace
{

public static void main(String args[])
{
    String arr[]=new String[3];
    arr[0]="Google";
    arr[1]="Apple";
    arr[2]="Amazon";
    for(int i=0;i<arr.length;i++)
    {
        int j;
        for(j=1;j<arr[i].length();j++)
        {
            if(arr[i].charAt(j) == arr[i].charAt(j-1))
            {
                break;
            }
        }
        if(j==arr[i].length())
                System.out.println(arr[i]);
    }
}
}

逻辑:匹配中的字符


  1. 如果找到string [i] == string [i-1]。打破循环。选择下一个字符串。

  2. 如果直到字符串末尾都没有连续重复的重复字符,则打印该字符串。

这篇关于检查字符串中连续的重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆