string inputData ="AabbbCdd"
StringBuilder strb = new StringBuilder();
char[] char_str = inputData.ToCharArray();
//char cr = char_str[0];
bool falg = true;
for (int i=0;i<= char_str.Length - 1; i++)
{
if(i== char_str.Length - 2)
{
if (char_str[i].ToString().ToUpper() == char_str[i + 1].ToString().ToUpper())
{
if (falg)
{
strb.Append(char_str[i]);
strb.Append(char_str[i + 1]);
falg = false;
}
else
{
strb.Append(char_str[i + 1]);
}
}
else
{
falg = true;
}
break;
}
if (char_str[i].ToString().ToUpper()== char_str[i+1].ToString().ToUpper())
{
if (falg)
{
strb.Append(char_str[i]);
strb.Append(char_str[i + 1]);
falg = false;
}
else
{
strb.Append(char_str[i + 1]);
}
}
else
{
falg = true;
}
}
newWord = strb.ToString();
//输出 Aabbbdd
string newWord = "Aabbbdd";
List<string> strlist = new List<string>();
char[] chstr = newWord.ToCharArray();
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (char c in chstr)
{
string str = c.ToString().ToUpper();
if (!dic.ContainsKey(str))
{
dic.Add(str, 1);
}
else
{
dic[str] = dic[str] + 1;
}
}
Dictionary<string, int> dic1_SortedByKey = dic.OrderByDescending(o => o.Value.ToString()).ToDictionary(p => p.Key, o => o.Value);
int a = 0;
foreach (string st in dic1_SortedByKey.Keys)
{
if (dic1_SortedByKey[st] == a)
{
char c =Convert.ToChar(strlist[strlist.Count - 1]);
if (Convert.ToChar(st) < c)
{
strlist.Insert(strlist.Count - 1,st);
}
else
{
strlist.Add(st);
}
}
else
{
strlist.Add(st);
}
a = dic1_SortedByKey[st];
}
finalWord = string.Join("", strlist);
输出bad
//String str = "aaaccccbbbbAnhnnnNNNGg";
//List<String> list = getSubs(str, 0);
//foreach (var item in list)
//{
// Console.WriteLine(item);
//}
/**
* 统计连续相同字符的子串
* @param str
* @param idx
* @return
*/
public static List<string> getSubs(string str, int idx)
{
List<string> result = new List<string>();
if (str != null)
{
int len = str.Length;
if (len > 0)
{
string sub = subStr(str, idx);
Console.WriteLine("sub => " + sub);
int tLen = sub.Length;
if (tLen > 1)
{
result.Add(sub);
}
if (idx < len - 1)
{
int endIdx = idx + tLen;
if (endIdx < len - 1)
{
result.AddRange(getSubs(str, endIdx));
}
}
}
}
return result;
}
/**
* 获取连续相同字符子串
* @param str
* @param idx
* @return
*/
public static string subStr(string str, int idx)
{
char c = str[idx];
StringBuilder result = new StringBuilder();
result.Append(c);
if (idx < str.Length - 1)
{
if (c == str[idx + 1])
{
result.Append(subStr(str, idx + 1));
}
}
return result.ToString();
}
public class Solution {
public string FrequencySort(string s) {
Dictionary<char, int> dictionary = new Dictionary<char, int>();
int length = s.Length;
for (int i = 0; i < length; i++) {
char c = s[i];
if (dictionary.ContainsKey©) {
dictionary[c]++;
} else {
dictionary.Add(c, 1);
}
}
List list = new List(dictionary.Keys);
list.Sort((a, b) => dictionary[b] - dictionary[a]);
StringBuilder sb = new StringBuilder();
int size = list.Count;
for (int i = 0; i < size; i++) {
char c = list[i];
int frequency = dictionary[c];
for (int j = 0; j < frequency; j++) {
sb.Append©;
}
}
return sb.ToString();
}
}
/* A出现了4次
B出现了7次
C出现了5次
M出现了7次
N出现了1次
a出现了1次
d出现了2次
c出现了1次
f出现了1次
s出现了2次
*/
string str = "AAAABBBBBBBCCCCCMMMMMMMNadcdfss";
Dictionary<char, int> dic = new Dictionary<char, int>();
for (int i = 0; i < str.Length; i++)
{
//如果key已经包含了某个字符 则不再添加 让它的value加一
if (dic.Keys.Contains(str[i]))
{
dic[str[i]]++;
}
else
{
//不包含 则添加到字典中 并设置value为1
dic.Add(str[i], 1);
}
}
foreach (KeyValuePair<char, int> item in dic)
{
Console.WriteLine("{0}出现了{1}次", item.Key, item.Value);
}
Console.Read();
/* AAAA出现了2次
BBBBBBB出现了1次
CCCCC出现了1次
MMMMMMM出现了1次
N出现了1次
a出现了1次
d出现了2次
c出现了1次
f出现了1次
ss出现了1次
*/
string str = "AAAABBBBBBBCCCCCMMMMMMMNadcdfssAAAA";
Dictionary<string, int> dic = new Dictionary<string, int>();
string tempString = string.Empty;
string tempChar = string.Empty;
for (int i = 0; i < str.Length; i++)
{
if (tempChar == str[i].ToString())
{
//tempString += str[i].ToString();
}
else
{
tempChar = str[i].ToString();
if (!string.IsNullOrEmpty(tempString))
{
//如果key已经包含了某个字符 则不再添加 让它的value加一
if (dic.Keys.Contains(tempString))
{
dic[tempString]++;
}
else
{
//不包含 则添加到字典中 并设置value为1
dic.Add(tempString, 1);
}
tempString = string.Empty;
}
}
tempString += tempChar;
//最后一次循环的结果添加入字典
if (i == str.Length - 1)
{
//如果key已经包含了某个字符 则不再添加 让它的value加一
if (dic.Keys.Contains(tempString))
{
dic[tempString]++;
}
else
{
//不包含 则添加到字典中 并设置value为1
dic.Add(tempString, 1);
}
}
}
foreach (KeyValuePair<string, int> item in dic)
{
Console.WriteLine("{0}出现了{1}次", item.Key, item.Value);
}
Console.Read();
/// <summary>
/// c#找出字符串中出现最多的字符及次数
/// </summary>
private static void GetMostChar()
{
string str = "aaabbbccceeefff111144444";
Dictionary<char, int> chardict = new Dictionary<char, int>();
int maxcount = 0;
char x = 'a';
foreach (char a in str)
{
if (chardict.ContainsKey(a))
{
chardict[a] += 1;
if (maxcount < chardict[a])
{
maxcount = chardict[a];
x = a;
}
}
else
{
chardict.Add(a, 1);
}
}
Console.WriteLine(x.ToString());
Console.WriteLine(maxcount);
}
统计重复个数
string aa = "ABCAAABBCBHHHHBAEEEHAABJBCCABCAEFGHIJGHIJJJJI";
var r = aa.GroupBy(x => x);
foreach (var x in r) Console.WriteLine("{0} => {1}", x.Key, x.Count());
解决方案一:
我们使用系统内置的一个元素去重方法Distinct
string a = "abcabcd";
char[] b = a.ToArray().Distinct().ToArray();
string c = string.Join("", b);
解决方案二:
ArrayList alist = new ArrayList();
char[] strs = a.ToArray();
for (int i = 0; i < strs.Length; i++)
{
if (!alist.Contains(strs[i]))
{
alist.Add(strs[i]);
}
}
去重三:
/// <summary>
/// 去除相邻重复的字符串功能
/// </summary>
/// <param name="inputData">输入字符串(aaabccd\ddbbc)</param>
/// <returns>去除相邻重复的字符串结果(abcd\dbc)</returns>
public string GetRemoveOverlapStr(string inputData)
{
string strResult = "";
strResult = inputData[0].ToString();
for (int i = 1; i < inputData.Length; i++)
{
if (strResult[strResult.Length-1] == inputData[i])
{
continue;
}
else
{
strResult += inputData[i];
}
}
return strResult;
}
给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数。
注意:输出结果可能非常大,所以你需要返回一个字符串而不是整数。
示例 1:
输入:nums = [10,2]
输出:“210”
示例 2:
输入:nums = [3,30,34,5,9]
输出:“9534330”
public class Solution {
public string LargestNumber(int[] nums)
{
List<string> numList = new List<string>();
for (int i = 0; i < nums.Length; i++)
{
numList.Add(nums[i].ToString());
}
numList.Sort((x,y)=>
{
if (IsSame(x, y)) return 0;
int xPos = 0;int yPos = 0;
int lastX=x[xPos] - '0'; int lastY=y[yPos] - '0';
while (xPos<=x.Length-1||yPos<=y.Length-1)
{
if (lastX > lastY)
return -1;
else if (lastX < lastY)
return 1;
else
{
lastX = x[xPos + 1 >= x.Length ? xPos-= xPos : ++xPos]-'0';
lastY = y[yPos + 1 >= y.Length ? yPos -= yPos : ++yPos] - '0';
if ((xPos==0)&&(x.Length==y.Length))
return 0;
}
}
return 0;
});
StringBuilder answer = new StringBuilder();
for (int i = 0; i < numList.Count; i++)
{
answer.Append(numList[i]);
}
int anwer1 = 1;
if (int.TryParse(answer.ToString(), out anwer1))
return anwer1.ToString();
else
return answer.ToString();
}
bool IsSame(string a,string b)
{
for (int i = 0; i < a.Length-1; i++)
{
if (a[i] != a[i + 1]) return false;
}
for (int i = 0; i < b.Length-1; i++)
{
if (b[i] != b[i + 1]) return false;
}
return a[0] == b[0] ? true : false;
}
}
给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。
示例 1:
输入:s = “bcabc”
输出:“abc”
示例 2:
输入:s = “cbacdcbc”
输出:“acdb”
public class Solution {
public string RemoveDuplicateLetters(string s) {
bool[] added = new bool[26]; //字符已经加入了,此值为真
int[] nums = new int[26]; //字符出现过的次数
for (int i = 0; i < s.Length; i++)//统计字符出现次数
{
nums[s[i] - 'a']++;
}
List<char> ansL = new List<char>();
for (int i = 0; i < s.Length; i++)
{
char ch = s[i];
if (!added[ch - 'a'])//如果还未加入,就做如下处理
{
while (ansL.Count > 0 && ansL[ansL.Count - 1] > ch)//只要列表最后的元素更大,并且后面还会出现,就移除最后这个字符
{
if (nums[ansL[ansL.Count - 1] - 'a'] > 0)//出现次数仍大于零,表示以后还会出现
{
added[ansL[ansL.Count - 1] - 'a'] = false;//如果该字符被移除了立即改成未加入状态,也就是变成了新字符
ansL.RemoveAt(ansL.Count - 1);//移除最后一个字符
}
else
{
break;
}
}
added[ch - 'a'] = true;//已经加入了,变成旧字符。
ansL.Add(ch);//加入这个字符
}
nums[ch - 'a']--;//出现次数如果递减到0,就表示以后不会再出现了
}
return new string(ansL.ToArray());
}
}
/// <summary>
/// 找出相邻一样的字符串个数 "sea", "eat"
/// </summary>
/// <param name="word1">"sea"</param>
/// <param name="word2">"eat"</param>
/// <returns> 2 </returns>
public static int MinDistance(string word1, string word2)
{
int m = word1.Length, n = word2.Length;
int[,] dp = new int[m + 1, n + 1];
for (int i = 1; i <= m; i++)
{
char c1 = word1[i - 1];
for (int j = 1; j <= n; j++)
{
char c2 = word2[j - 1];
if (c1 == c2)
{
dp[i, j] = dp[i - 1, j - 1] + 1;
}
else
{
dp[i, j] = Math.Max(dp[i - 1, j], dp[i, j - 1]);
}
}
}
int lcs = dp[m, n];
return m - lcs + n - lcs;
}
无重复字符的最长子串
输入: s = “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3
输入: s = “pwwkew”
输出: 3
解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。
请注意,你的答案必须是 子串 的长度,“pwke” 是一个子序列,不是子串。
public int LengthOfLongestSubstring(string s) {
List<char> ls = new List<char>();
int n = s.Length;
int intMaxLength = 0;
for (int i = 0; i < n; i++)
{
if (ls.Contains(s[i]))
{
ls.RemoveRange(0, ls.IndexOf(s[i]) + 1);
}
ls.Add(s[i]);
intMaxLength = ls.Count > intMaxLength ? ls.Count : intMaxLength;
}
return intMaxLength;
}
整数反转
输入:x = 123
输出:321
输入:x = 120
输出:21
输入:x = 0
输出:0
public class Solution {
public int Reverse(int x) {
int rev = 0;
while (x != 0) {
if (rev < int.MinValue / 10 || rev > int.MaxValue / 10) {
return 0;
}
int digit = x % 10;
x /= 10;
rev = rev * 10 + digit;
}
return rev;
}
}