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. Go怎么获取当前时间? Go ARM64 vDSO优化之路

    https://mzh.io/ Go ARM64 vDSO优化之路 2018-03-16  | Meng Zhuo 背景 Go怎么获取当前时间?问一个会Go的程序员,他随手就能写这个出来给你. imp ...

  2. send data to Flume client-sdk flume使用之httpSource

    https://flume.apache.org/FlumeDeveloperGuide.html#client-sdk flume使用之httpSource - CSDN博客 https://blo ...

  3. Building REST services with Spring

    https://spring.io/guides/tutorials/bookmarks/

  4. tornado之模板扩展

    当我们有多个模板的时候,很多模板之间其实相似度很高.我们期望可以重用部分网页代码.这在tornado中可以通过extends语句来实现.为了扩展一个已经存在的模板,你只需要在新的模板文件的顶部放上一句 ...

  5. OpenMeetings安装

    OpenMeetings是一个开源的视频会议软件. 它是基于OpenLaszlo’s的新流媒体格式和开源的Flash服务器---Red5! 采用了flash流媒体服务器Red5+OpenMeeting ...

  6. linux定期判断网站可否打开

      wget http://www.shopindream.de/200.php --timeout=2 c_monitor=$? rm -rf 200.php if [ ! $c_monitor = ...

  7. 【R】R语言生成随机数

    1.概述 作为一种语言进行统计分析,R有一个随机数生成各种统计分布功能的综合性图书馆.R语言可以针对不同的分布,生成该分布下的随机数.其中,有许多常用的个分布可以直接调用.本文简单介绍生成常用分布随机 ...

  8. Struts action

    <action name="KnowledgeBankManageAction_*" class="knowledgeBankManageAction" ...

  9. git项目.gitignore文件不生效解决办法

    配置好.gitignore文件如下: HELP.md /target/ !.mvn/wrapper/maven-wrapper.jar ### STS ### .apt_generated .clas ...

  10. Linux - Unix环境高级编程(第三版) 源代码编译(即头文件apue.h如何使用问题)【转】

    本文转载自:http://blog.csdn.net/hadas_wang/article/details/43203795 1. 下载代码:http://www.apuebook.com/code3 ...