在codewars里升级吧~3:一道6级题代码及讲解视频

12 篇文章 0 订阅
8 篇文章 0 订阅

今天讲一道 codewars 6级题。

纯手工视频,无修正,希望下期能讲难一点的题。不过算法题讲起来都是数学问题,想抛掉数学写代码还是难啊。

 

腾讯讲解视频链接

https://v.qq.com/x/page/v0715ap9lfa.html

 

B站讲解视频链接

https://www.bilibili.com/video/av26409430

 

题目

Count the number of Duplicates

Write a function that will return the count of distinct case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits.

 

Example

"abcde" -> 0 # no characters repeats more than once

"aabbcde" -> 2 # 'a' and 'b'

"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (bandB)

"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

 

SOLUTION

 

 size_t duplicateCount(const std::string& in); // helper for tests

#include <array>
#include <string>

size_t duplicateCount(const char* in)
{
    
  std::string data(in);
  std::array<int, 26> Count;
  Count.fill(0);
  for (char i : data) {
    if (i - 'a' >= 0 && i - 'a' < 26) {
      Count[i - 'a']++;
    }
    else if (i - 'A' >= 0 && i - 'A' < 26) {
      Count[i - 'A']++;
    }
  }
  int ret = 0;
  for (int i : Count) {
    ret += int(i > 1);
  }
  return ret;

}

欢迎关注微信公众号:小霍编程

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值