心如止水

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

/// <summary>

/// Uses letterCount array to record each letter's cout, count of letter 'a' stored in letterCount[0],

/// count of letter 'b' stored in letterCount[1], count of letter 'c' stored in letterCount[2], etc.

/// The complexity of this algorism is O(n).

/// </summary>

/// <param name="s"></param>

private static void FindFirstNotDuplicatedLetter(string s)

{

int[] letterCount = new int[26];

for (int i = 0; i < s.Length; i++)

{

letterCount[s[i] - 'a']++;

}

 

bool found = false;

for (int i = 0; i < s.Length; i++)

{

if (letterCount[s[i] - 'a'] == 1)

{

found = true;

Console.WriteLine(s[i]);

break;

}

}

 

if (!found)

Console.WriteLine("No not duplicated letter.");

}

posted on 2012-04-26 16:30  cutebear  阅读(470)  评论(0编辑  收藏  举报