Skip to content

Commit 053d2c6

Browse files
committed
string tag
1 parent 04a3b3b commit 053d2c6

15 files changed

+574
-0
lines changed

Stack/Stack/Stack.v12.suo

512 Bytes
Binary file not shown.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace StringSln.Lib
8+
{
9+
public class DetectCapitalSln
10+
{
11+
// Given a word, you need to judge whether the usage of capitals in it is right or not.
12+
13+
//We define the usage of capitals in a word to be right when one of the following cases holds:
14+
15+
//All letters in this word are capitals, like “USA”.
16+
//All letters in this word are not capitals, like “leetcode”.
17+
//Only the first letter in this word is capital if it has more than one letter, like “Google”.
18+
//Otherwise, we define that this word doesn’t use capitals in a right way.
19+
20+
//Example 1:
21+
//Input: "USA"
22+
//Output: True
23+
24+
//Example 2:
25+
//Input: "FlaG"
26+
//Output: False
27+
28+
public bool DetectCapitalUse(string word)
29+
{
30+
if (string.IsNullOrEmpty(word)) return true;
31+
32+
char fst = word[0];
33+
if (charCapital(fst))
34+
{
35+
int afterCapital = 0;
36+
for (int i = 1; i < word.Length; i++)
37+
{
38+
if (charCapital(word[i]))
39+
afterCapital++;
40+
}
41+
if (afterCapital == 0)
42+
return true;//只有首字符大写
43+
else if (afterCapital == word.Length - 1)
44+
return true;//都是大写
45+
return false;
46+
}
47+
else//首字符小写
48+
{
49+
int afterCapotal = 0;
50+
for (int i = 1; i < word.Length; i++)
51+
{
52+
if (charCapital(word[i]))
53+
afterCapotal++;
54+
}
55+
return afterCapotal == 0 ? true : false;
56+
}
57+
58+
}
59+
60+
private bool charCapital(char ch)
61+
{
62+
int chint = Convert.ToInt32(ch);
63+
if (chint >= 65 && chint <= 90)
64+
return true;
65+
return false;
66+
}
67+
}
68+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace StringSln.Lib
8+
{
9+
// Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.
10+
11+
//If the last word does not exist, return 0.
12+
13+
//Note: A word is defined as a character sequence consists of non-space characters only.
14+
15+
//For example,
16+
17+
//Given s = "Hello World",
18+
//return 5.
19+
public class LengthOfLastWordSln
20+
{
21+
public int LengthOfLastWord(string s)
22+
{
23+
s = s.Trim();
24+
if (s == "") return 0;
25+
for (int i = s.Length - 1; i > 0; i--)
26+
{
27+
if (s[i] == ' ')
28+
return s.Length - i - 1;
29+
}
30+
return s.Length;
31+
}
32+
public int LengthOfLastWord2(string s)
33+
{
34+
s = s.Trim(); //去掉前,后空格
35+
if (s == "") return 0;
36+
string[] splitstring = s.Split(' ');
37+
string slast = splitstring[splitstring.Length - 1];
38+
return slast.Length == 0 ? 1 : slast.Length;
39+
}
40+
}
41+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace StringSln.Lib
8+
{
9+
public class LongestCommonPrefixSln
10+
{
11+
public string LongestCommonPrefix(string[] strs)
12+
{
13+
if (strs == null || strs.Length == 0)
14+
return "";
15+
if (strs.Length == 1)
16+
return strs[0];
17+
18+
string longestPrefix = firstTwoStrs(strs);
19+
if (longestPrefix.Length == 0)
20+
return "";
21+
int i = 2;
22+
while (i < strs.Length)
23+
{
24+
if (strs[i].IndexOf(longestPrefix) != 0)
25+
{
26+
longestPrefix =
27+
longestPrefix.Substring(0, longestPrefix.Length - 1);
28+
i = 1;
29+
if (longestPrefix.Length == 0)
30+
return "";
31+
}
32+
i++;
33+
}
34+
return longestPrefix;
35+
}
36+
37+
//头两个字符串的公共串
38+
private string firstTwoStrs(string[] strs)
39+
{
40+
if (strs.Length < 2) return "";
41+
StringBuilder sb = new StringBuilder();
42+
int len = strs[0].Length < strs[1].Length ? strs[0].Length : strs[1].Length;
43+
for (int i = 0; i < len; i++)
44+
{
45+
if (strs[0][i] == strs[1][i])
46+
sb.Append(strs[0][i]);
47+
else
48+
break;
49+
}
50+
return sb.ToString();
51+
}
52+
}
53+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// 有关程序集的常规信息通过以下
6+
// 特性集控制。更改这些特性值可修改
7+
// 与程序集关联的信息。
8+
[assembly: AssemblyTitle("StringSln.Lib")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("StringSln.Lib")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// 将 ComVisible 设置为 false 使此程序集中的类型
18+
// 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
19+
// 则将该类型上的 ComVisible 特性设置为 true。
20+
[assembly: ComVisible(false)]
21+
22+
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
23+
[assembly: Guid("3f2eba77-d506-477a-ac13-da11cc28296b")]
24+
25+
// 程序集的版本信息由下面四个值组成:
26+
//
27+
// 主版本
28+
// 次版本
29+
// 生成号
30+
// 修订号
31+
//
32+
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
33+
// 方法是按如下所示使用“*”:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

String/StringSln.Lib/RansomNoteSln.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace StringSln.Lib
8+
{
9+
// Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
10+
11+
//Each letter in the magazine string can only be used once in your ransom note.
12+
13+
//Note:
14+
//You may assume that both strings contain only lowercase letters.
15+
16+
//canConstruct("a", "b") -> false
17+
//canConstruct("aa", "ab") -> false
18+
//canConstruct("aa", "aab") -> true
19+
//canConstruct("fffbfg","effjfggbffjdgbjjhhdegh") ->true
20+
public class RansomNoteSln
21+
{
22+
public bool CanConstruct(string ransomNote, string magazine)
23+
{
24+
if (magazine == null && ransomNote == null) return true;
25+
if (magazine == null) return false;
26+
27+
//magazine字符串整理为字典
28+
Dictionary<char, int> dict = new Dictionary<char, int>();
29+
foreach (var item in magazine)
30+
{
31+
if (dict.ContainsKey(item))
32+
dict[item]++;
33+
else dict.Add(item, 1);
34+
}
35+
36+
foreach (var item in ransomNote)
37+
{
38+
if (!dict.ContainsKey(item))//未出现直接返回false
39+
return false;
40+
dict[item]--;
41+
if (dict[item] == 0) //为0后,移除
42+
dict.Remove(item);
43+
}
44+
45+
return true;
46+
}
47+
}
48+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace StringSln.Lib
8+
{
9+
public class RepeatedSubstringSln
10+
{
11+
// Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
12+
13+
//Example 1:
14+
//Input: "abab"
15+
16+
//Output: True
17+
18+
//Explanation: It's the substring "ab" twice.
19+
20+
//Example 2:
21+
//Input: "aba"
22+
23+
//Output: False
24+
25+
//Example 3:
26+
//Input: "abcabcabcabc"
27+
28+
//Output: True
29+
30+
//Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
31+
32+
public bool RepeatedSubstringPattern(string s)
33+
{
34+
for (int i = s.Length / 2; i > 0; i--)
35+
{
36+
if (s.Length % i != 0)
37+
continue;
38+
string prefix = s.Substring(0, i); //可能的重复子串
39+
string strsub = s.Substring(i); //去掉以上可能的重复子串后
40+
int index = strsub.IndexOf(prefix);
41+
if (index != 0)
42+
continue;
43+
else //表明紧接着字符串匹配上
44+
{
45+
//用这个子串重复s.Length/i次,若与原串相等,便为true。
46+
StringBuilder sb = new StringBuilder();
47+
for (int j = 0; j < s.Length / i; j++)
48+
sb.Append(prefix);
49+
if (sb.ToString() == s)
50+
return true;
51+
}
52+
53+
}
54+
return false;
55+
}
56+
}
57+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace StringSln.Lib
8+
{
9+
public class SegmentNumberSln
10+
{
11+
// Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
12+
13+
//Please note that the string does not contain any non-printable characters.
14+
15+
//Example:
16+
17+
//Input: "Hello, my name is John"
18+
//Output: 5
19+
20+
//Input: " "
21+
//Output: 0
22+
23+
//Input: " a"
24+
//Output: 1
25+
26+
//Input: " a "
27+
//Output: 1
28+
29+
//Input: " a b "
30+
//Output: 2
31+
32+
public int CountSegments(string s)
33+
{
34+
if (string.IsNullOrEmpty(s))
35+
return 0;
36+
int i = 0;
37+
while (i < s.Length && !isValidBegin(s[i]))
38+
i++;
39+
if (i == s.Length) //没有有效字符,如" "
40+
return 0;
41+
if (i == s.Length - 1) //只有一个有效字符,如 " a"
42+
return 1;
43+
while (i < s.Length - 1 && !isValidEnd(s[i], s[i + 1]))
44+
i++;
45+
if (i == s.Length - 1)
46+
return 1; //只有1个有效字符,如" a "
47+
48+
return 1 + CountSegments(s.Substring(i + 1));
49+
}
50+
51+
//字符串的第一个片段的首字符,找到有效字符开始
52+
private bool isValidBegin(char curchar)
53+
{
54+
if (curchar != ' ')
55+
return true;
56+
return false;
57+
}
58+
59+
60+
//判断字符片段的结束
61+
private bool isValidEnd(char curchar, char afterchar)
62+
{
63+
if (curchar != ' ' && afterchar == ' ')
64+
return true;
65+
return false;
66+
}
67+
68+
69+
}
70+
}

0 commit comments

Comments
 (0)