Easy to understand C++ solution


  • 0
    M
    class Solution 
    {
    	unordered_map <char, char> mp = { {'1', '1'},  {'0', '0'} ,{ '6', '9'},{ '9', '6'}, {'8', '8'} };
    	bool isvalid(char ip)
    	{
    		if(mp[ip])
    			return true;
    		return false;
    	}
    	bool issame(string num1, string num2)
    	{
    		int i = 0;
    		while (i < num1.length())
    		{
    			if (num1[i] != num2[num2.length() - 1 - i])
    				return false;
    			i++;
    		}
    		return true;
    	}
    	string prepare_number(string s1)
    	{
    		string output;
    		for (int i = 0; i < s1.length(); i++)
    		{
    			char ch = mp[s1[i]];
    			output += ch;
    		}
    		return output;
    	}
    	public:
    		bool isStrobogrammatic(string num) 
    		{
    			for (int i = 0; i < num.length(); i++)
    			{
    				if (!isvalid(num[i]))
    					return false;
    			}
    			string num2 = prepare_number(num);
    			return issame(num, num2);
    			return true;
    		}
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.