[프로그래머스] 전화번호 목록
Algorithm 2023. 1. 20. 23:59https://school.programmers.co.kr/learn/courses/30/lessons/42577?language=cpp
#include <iostream>
#include <vector>
#include <unordered_map>
#include <string>
using namespace std;
bool solution(vector<string> phone_book) {
unordered_map<string, int> map;
for(int i =0 ; i<phone_book.size(); i++)
map[phone_book[i]] = 1;
for(int i = 0; i<phone_book.size(); i++){
string phone_number = "";
for(int j = 0; j<phone_book[i].size(); j++){
phone_number += phone_book[i][j];
if(map[phone_number] && phone_number != phone_book[i])
return false;
}
}
return true;
}
int main()
{
return 0;
}
C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Programmers
{
class Program
{
static void Main()
{
//
//List<string> phoneBook = new List<string>(new string[]{"119", "97674223", "1195524421"});
List<string> phoneBook = new List<string>(new string[]{"123","456","789"});
var result = new Solution().solution(phoneBook);
Console.WriteLine(result);
}
}
class Solution
{
public bool solution(List<string> phone_book)
{
bool answer = true;
Dictionary<string, int> dic = new Dictionary<string, int>();
for (int i = 0; i < phone_book.Count; i++)
{
dic.Add(phone_book[i], 1);
}
for (int i = 0; i < phone_book.Count; i++)
{
var phone_number = "";
var len = phone_book[i].Length;
for (int j = 0; j < len; j++)
{
phone_number += phone_book[i][j];
if (dic.ContainsKey(phone_number) && phone_number != phone_book[i])
answer = false;
}
}
return answer;
}
}
}
'Algorithm' 카테고리의 다른 글
C#으로 구현한 우선순위 큐 Priority Queue (0) | 2023.01.24 |
---|---|
[BOJ] 2684 동전 게임 (0) | 2023.01.21 |
[프로그래머스] 다트게임 (0) | 2023.01.20 |
[BOJ] 21921 블로그 (0) | 2023.01.20 |
[BOJ] 1940 주몽 (0) | 2023.01.20 |