字母 非重复字符串个数(Java)

题目来源

https://www.nowcoder.com/exam/test/83198992/detail?pid=54768478

其中的第291题

题目内容如下图

我的代码如下

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();

        int cnt=0;
        for(char ch='a';ch<='z';ch++){
            if(str.indexOf(ch)!=-1 && str.indexOf(ch) == str.lastIndexOf(ch)){
                cnt++;
            }
        }
        for(char ch='A';ch<='Z';ch++){
            if(str.indexOf(ch)!=-1 && str.indexOf(ch) == str.lastIndexOf(ch)){
                cnt++;
            }
        }

        System.out.println(cnt);
    }
}

运行结果

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
废物一个0offer:认真的吗二本本科找人工智能岗位
点赞 评论 收藏
分享
07-02 13:50
闽江学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务