forked from xieqilu/Qilu-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path71.SecondLongestStrInArray.cs
More file actions
55 lines (45 loc) · 1.37 KB
/
Copy path71.SecondLongestStrInArray.cs
File metadata and controls
55 lines (45 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//find second longest string from a string array
//iterate the array twice, use first iterate to find the
//longest string
//Then use second iterate to find the second longest string
//clarify question: will the strings in the array have same length?
//if there are two strings having the longest length, then which one
//is the second longest or there is no second longest string?
using System;
using System.Collections.Generic;
using System.Collections;
namespace SecondLongestStringInArray
{
public class Finder
{
public static string FindSecondLongestStr(string[] strs) //O(n)
{
if (strs.Length < 2)
return null;
string longestStr = string.Empty; //string.Empty equals to ""
string SecLongestStr = string.Empty;
foreach (string s in strs) { //O(n)
if (s.Length > longestStr.Length)
longestStr = s;
}
foreach (string s in strs) { //O(n)
if (s.Length > SecLongestStr.Length && s.Length != longestStr.Length)
SecLongestStr = s;
}
return SecLongestStr;
}
}
class MainClass
{
public static void Main (string[] args)
{
string[] strs = new string[]{ "aa", "bbb", "ccc", "c", "asdasdas", "asdasw", "bdsd" };
//string[] strs = new string[]{"asdas"};
string result = Finder.FindSecondLongestStr (strs);
if (result != null)
Console.WriteLine (result);
else
Console.WriteLine ("input array is too short");
}
}
}