序列码中的字母相邻对

时间:2018-05-29 12:59:17

标签: java

序列中的字母相邻对

接受一个仅包含小写字母的字符串S并打印实例C的计数,在一对相邻字符中,右字符位于原始字母序列中左侧字符旁边。

边界条件: 1 <=长度S <= 1000

输入格式: 第一行包含S.

输出格式: 第一行包含整数值C

示例输入/输出1: 输入: abegh

输出: 2

说明: ab gh是两个例子。

示例输入/输出2: 输入: ABCDEF

输出: 5

说明: ab bc cd de ef是五个实例。

我的解决方案:

import java.util.*;
public class adj{
public static void main(String args[])
{
    Scanner sc=new Scanner (System.in);
    String s=sc.next();
    char t,t1;
    String r[]=new String[100];
    char a[]={'a', 'b', 'c', 'd', 'e', 'f','g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r','s', 'u', 'v', 'w', 'x', 'y' ,'z'};
    int count=0;
    for(int i=0;i<(s.length()-1);i++)
    {
        t=s.charAt(i);
        t1=s.charAt(i+1);
        //r[i]=t+t1;
        if(t==(a[i]))
        { 
            if(t1==(a[i+1]))
                {
                    count++;
                    continue;
                }
            continue;
        }
    }
    System.out.print(count);
  }
}

但是此代码仅打印1个测试用例abegh而不是2

3 个答案:

答案 0 :(得分:1)

简洁!

#include<stdio.h>
#include <stdlib.h>

int main()
 {
char s[1002];
scanf("%s",s);
int ac=0;
int an = strlen(s);
for(int i=0;i<an-1;i++)
{
  if((s[i+1]-s[i])==1){
    ac+=1;
  }
}
printf("%d",ac);
}

答案 1 :(得分:0)

您只检查字母表中同一位置的字母。

abcdefghij&lt; - &#39; a&#39;有索引[0],&#39; g&#39;有索引[6]

abegh&lt; - &#39; a&#39;有索引[0],&#39; g&#39;有索引[3]

所以你要检查&#39; a&#39;但是不要检查&#39;

在您的代码中,您需要替换

for(int j=0;j<(a.length()-1);j++)
    {
        if (t == a[j]){
            if(t1==(a[j+1]))
                {
                    count++;
                    break;
                }
            continue;
        }
    }

for a in tree:
    for c in a:
        for b in c:
            b.text = '11111' + b.text
            break
        break
    break

对于这个特殊问题有更简单的解决方案,例如,您可以使用hashmap,这会使您的代码更快。但由于它是一项学校作业,我认为这并不重要。

底线是:对于你的方法,你需要内循环来翻看字母。

答案 2 :(得分:0)

无论何时开发算法,都可以使用TDD。一步一步地实现功能,确保在添加新的生产代码后通过所有测试。对于你的作业,我提出了以下代码(输入长度​​条件没有实现,因为我显然错过了那个,但你可以自己实现):

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class CharsTest {

    private class Chars {

        private void validateInput(String input) {
            if (!input.matches("^[a-z]*$")) {
                throw new IllegalArgumentException("Input only accepts lowercase alphabetic characters.");
            }
        }

        public int count(String input) {
            validateInput(input);

            int counter = 0;

            for (int i = 0; i < input.length() - 1; ++i) {
                if (input.charAt(i) + 1 == input.charAt(i + 1)) {
                    counter++;
                }
            }

            return counter;
        }
    }

    private Chars chars = new Chars();

    @Test
    public void shouldReturnZeroOnEmptyString() {
        String input = "";
        int actual = chars.count(input);
        assertEquals(0, actual);
    }

    @Test
    public void shouldReturnZeroOnNonEmptyStringWithZeroSequences() {
        String input = "zyxcba";
        int actual = chars.count(input);
        assertEquals(0, actual);
    }

    @Test
    public void shouldRejectUppercaseAlpha() {
        try {
            chars.count("ABC");
            fail("Expected an exception.");
        } catch (IllegalArgumentException e) {
            // ok
        }
    }

    @Test
    public void shouldRejectNonAlpha() {
        try {
            chars.count("123 $%^");
            fail("Expected an exception.");
        } catch (IllegalArgumentException e) {
            // ok
        }
    }

    @Test
    public void shouldReturnOneWhenOneSequenceIsFound() {
        String input = "acdgix";
        int actual = chars.count(input);
        assertEquals(1, actual);
    }

    @Test
    public void shouldCorrectlyCountAllSequences() {
        String input = "abcehpqtyz"; // ab, bc, pq, yz
        int actual = chars.count(input);
        assertEquals(4, actual);
    }
}

请注意,我采用了一种完全不同的方法来检查两个字符是否构成一个有效的对:我在字符串中取两个相邻的字符,比如AB,然后检查{{1} }正好比A少一个。由于B实际上是一个数值,因此您可以轻松确认字母字符的值是否为数字序列。