Question

You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S is a type of stone you have.  You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

Example:

Example 1:

Input: J = "aA", S = "aAAbbbb"
Output: 3

Example 2:

Input: J = "z", S = "ZZ"
Output: 0

Note:

  • S and J will consist of letters and have length at most 50.
  • The characters in J are distinct.

Answer One

class Solution {

    /**
* @param String $J
* @param String $S
* @return Integer
*/
function numJewelsInStones($J, $S) {
$Js = str_split($J);
$sum = 0;
foreach($Js as $v) {
$sum += substr_count($S, $v);
}
return $sum;
}
}

Answer Two

class Solution {

    /**
* @param String $J
* @param String $S
* @return Integer
*/
function numJewelsInStones($J, $S) {
// $Js = str_split($J);
$Ss = str_split($S);
$sum = 0;
$res = array_count_values($Ss);
foreach($res as $k => $v) {
if(strpos($J, $k) !== false) {
$sum += $v;
}
}
return $sum;
}
}

PHP 原生函数大法好...

01 | Jewels and Stones的更多相关文章

  1. LeetCode --> 771. Jewels and Stones

    Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...

  2. Leetcode#771.Jewels and Stones(宝石与石头)

    题目描述 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石. J 中的字母不重复,J 和 S中的所有字 ...

  3. 【Leetcode】Jewels and Stones

    Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...

  4. 【Leetcode_easy】771. Jewels and Stones

    problem 771. Jewels and Stones solution1: class Solution { public: int numJewelsInStones(string J, s ...

  5. 771. Jewels and Stones - LeetCode

    Question 771. Jewels and Stones Solution 题目大意:两个字符串J和S,其中J中每个字符不同,求S中包含有J中字符的个数,重复的也算 思路:Set记录字符串J中的 ...

  6. codechef Jewels and Stones 题解

    Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...

  7. 771. Jewels and Stones

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  8. [Swift]LeetCode771. 宝石与石头 | Jewels and Stones

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

  9. [LeetCode] Jewels and Stones 珠宝和石头

    You're given strings J representing the types of stones that are jewels, and S representing the ston ...

随机推荐

  1. sticky session 粘性会话

    New Elastic Load Balancing Feature: Sticky Sessions | AWS News Blog https://amazonaws-china.com/cn/b ...

  2. mongodb学习之:条件操作符

    在前面的章节中我们已经有用到了条件操作符,这里我们再重点介绍下.MongoDB中条件操作符有: (>) 大于 - $gt (<) 小于 - $lt (>=) 大于等于 - $gte ...

  3. ffmpeg xcode 中的使用

    最近比较闲,苦于ios设备上没有直接播放torrent 文件的软件,开始折腾了.找了不少资料有了思路.但是其中用到了ffmpeg 这个东西. ffmpeg 是通用的一个视频解决框架,用C语言编写,通用 ...

  4. httpd 隐藏文件

    问题情况, 因磁盘空间问题,使用rsync将 php工作目录下文件copy到新盘中后,出现 php服务很多目录访问返回 404,路径找不到,其实文件都存在,而且路径都是对的 解决思路. 根目录下 有个 ...

  5. Java 线程转储

    软件维护是一个枯燥而又有挑战性的工作.只要软件功能符合预期,那么这个工作就是好的.设想一个这样的情景,你的电话半夜也一直在响(这不是一个令人愉快的感受,是吧?)任何软件系统,无论它当初是被设计的多好, ...

  6. BestCoder6 1002 Goffi and Squary Partition(hdu 4982) 解题报告

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=530 (格式有一点点问题,直接粘 ...

  7. windows查看与清理dns缓存

    1.windows下在命令行输入  ipconfig /flushdns 清理本地dns缓存查看dns缓存   ipconfig/displaydns2.Mac OSX下在命令行输入  lookupd ...

  8. bzoj 1098 [POI2007]办公楼biu——链表

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1098 求补图的连通块大小.与自己没有边的和自己在一个连通块里. 用链表把所有点串起来.先给自 ...

  9. bzoj 2836 魔法树——树链剖分

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2836 树剖裸题.然而WA.RE了好久…… 原来是跳 top 的那个地方! top 不相等的时 ...

  10. CSS3:CSS 听觉参考手册

    ylbtech-CSS3:CSS 听觉参考手册 1.返回顶部 1. 听觉样式表 听觉样式表可把语音合成与音响效果相组合,使用户可以听到信息,而无需进行阅读. 听觉呈现可用于: 视觉能力低弱的人士 帮助 ...