
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to find length of country code from phone numbers
Suppose we have a list of strings S with n numeric strings. Amal has n friends in a city. Amal knows phone numbers of all his friends: they are stored in S. All strings in S are of same length. Once Amal needed to figure out the city phone code. He assumed that the phone code of the city is the longest common prefix of all phone numbers of his friends. We have to find the length of the city phone code.
Problem Category
To solve this problem, we need to manipulate strings. Strings in a programming language are a stream of characters that are stored in a particular array-like data type. Several languages specify strings as a specific data type (eg. Java, C++, Python); and several other languages specify strings as a character array (eg. C). Strings are instrumental in programming as they often are the preferred data type in various applications and are used as the datatype for input and output. There are various string operations, such as string searching, substring generation, string stripping operations, string translation operations, string replacement operations, string reverse operations, and much more. Check out the links below to understand how strings can be used in C/C++.
https://www.tutorialspoint.com/cplusplus/cpp_strings.htm
https://www.tutorialspoint.com/cprogramming/c_strings.htm
So, if the input of our problem is like S = ["00209", "00219", "00999", "00909"], then the output will be 2, because the code is "00"
Steps
To solve this, we will follow these steps −
n := size of S ans := 0 m := size of S[0] for initialize i := 0, when i < m, update (increase i by 1), do: c := S[0, i] for initialize j := 0, when j < n, update (increase j by 1), do: if S[j, i] is not equal to c, then: return ans (increase ans by 1) return ans
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<string> S){ int n = S.size(); int ans = 0; int m = S[0].size(); for (int i = 0; i < m; i++){ char c = S[0][i]; for (int j = 0; j < n; j++){ if (S[j][i] != c){ return ans; } } ans++; } return ans; } int main(){ vector<string> S = { "00209", "00219", "00999", "00909" }; cout << solve(S) << endl; }
Input
{ "00209", "00219", "00999", "00909" }
Output
2
- Related Articles
- How to set country code to column values with phone numbers in MySQL?
- How to add country/area code to a phone number list in Excel?
- C++ code to check phone number can be formed from numeric string
- How to get current country code from Network provider in android?
- C++ Program to find length of substring song from main song
- How to get default phone Network Country Iso in android?
- Program to find length of longest sign alternating subsequence from a list of numbers in Python
- How to change current country code in android?
- C++ code to find total number of digits in special numbers
- C++ code to find xth element after removing numbers
- C++ code to find minimum operations to make numbers c and d
- Get the count of unique phone numbers from a column with phone numbers declared as BIGINT type in MySQL
- C++ code to find rank of student from score table
- C++ code to find composite numbers whose difference is n
- C++ code to find three numbers whose sum is n
