-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseWord.cs
59 lines (46 loc) · 1.49 KB
/
ReverseWord.cs
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
56
57
58
59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReverseWord
{
class Program
{
static int ReversePairsCount(List<string> words)
{
//I reverse each word for compare with other items of list
int numberOfPairs = 0;
for(int i=0; i<words.Count-1;i++)
{
string revWord = ReverseWord(words[i]);
for (int j=i+1;j<words.Count;j++)
{
if ( revWord == words[j])
{
numberOfPairs++;
}
//I remove one of similar words
else if(words[i]==words[j])
{
words.Remove(words[j]);
}
}
}
return numberOfPairs;
}
//method which help I reverse words
static string ReverseWord(string s)
{
char[] array = s.ToCharArray();
Array.Reverse(array);
return new string(array);
}
static void Main(string[] args)
{
List<string> words = new List<string>() { "hi", "hello", "ih", "hi", "stop","olleh","pots","can" };
Console.WriteLine("Number of pairs = " + ReversePairsCount(words));
Console.Read();
}
}
}