zoukankan      html  css  js  c++  java
  • Codewars Solution:Counting Duplicates

    Level 6kyu :Counting Duplicates

    描述:

    计算重复次数编写一个函数,该函数将返回在输入字符串中多次出现的不区分大小写的字母字符和数字的计数。

    可以假定输入字符串仅包含字母(大写和小写)和数字。

    例如:

    "abcde" -> 0 # no characters repeats more than once
    "aabbcde" -> 2 # 'a' and 'b'
    "aabBcde" -> 2 # 'a' occurs twice and 'b' twice (`b` and `B`)
    "indivisibility" -> 1 # 'i' occurs six times
    "Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
    "aA11" -> 2 # 'a' and '1'
    "ABBA" -> 2 # 'A' and 'B' each occur twice

     1 public static int duplicateCount(String text) {
     2     // Write your code here
     3     text=text.toLowerCase();//要求大小写也算转为小写或者大写(toUpperCase()方法)
     4     int len=text.length();
     5     int total=0;//返回(有两个及以上字符相同的)个数
     6     char[] arr=new char[len];//装入已检查字符,后续出现的不再循环
     7     int index=0;
     8     for(int i=0;i<len;i++){
     9       boolean flag=true;
    10       int count=1;
    11       for(char c:arr){
    12         if(c==text.charAt(i)){
    13           flag=false;
    14         }
    15       }
    16       if(flag){//true即上面数组里面不存在现在循环的当前字符
    17         for(int j=i;j<len;j++){
    18           if(j==i)continue;
    19           else{
    20             if(text.charAt(i)==text.charAt(j)){
    21               count++;
    22             }
    23           }
    24         }
    25         arr[index++]=text.charAt(i);
    26       }
    27       if(count!=1){//不等于1即有重复字符,总个数加1
    28           total++;
    29       }
    30     }
    31     return total;
    32   

    总结:代码调试真是找错的好东西。:D

  • 相关阅读:
    mysql字符生命周期
    mysql5.6特殊字符问题
    电信网关-天翼网关-GPON-HS8145C设置桥接路由拨号认证
    linux-shell脚本高并发对文本url批量下载
    Kettle7.1在window启动报错
    微软的在线文档存储OneDrive使用帮助
    centos6.5搭建redmine3.4
    mysql基础拓扑图
    线上应用故障排查之一:高CPU占用
    线上服务CPU100%问题快速定位实战
  • 原文地址:https://www.cnblogs.com/mc-web/p/13179036.html
Copyright © 2011-2022 走看看