Leetcode

πŸ“ Problem Details

πŸ’­What Were My Initial Thoughts?

- convert the integer to a string
- iterate through the string, flip the first 6 we encounter to a 9
- return stoi(str)

πŸ€”What Did I Struggle With?

remembering the to_string function

πŸ’‘ Explanation of Solution

initial thoughts capture the correct approach

βŒ› Complexity Analysis

Time Complexity: O(n)
- we are conducting a single path through of the input array

Space Complexity: O(1)
- no additional space is required to solve this problem as we are manipulating the input string

πŸ’» Implementation of Solution

class Solution {
public:
	int maximum69Number(int num) {
		string numString = to_string(num);
		for(int i=0; i < numString.size(); i++) {
			if(numString[i] == '6') {
				numString[i] = '9';
				break;
			}
		}
		return stoi(numString);
	}
};