leetcode) 125. valid palindrome

Doongsil·2024년 5월 8일

✅ 입력이 ASCII characters 이므로
string index 에 직접 접근해도 무방

func isPalindrome(s string) bool {
	convertedStr := convert(s)
	sp := 0
	ep := len(convertedStr) - 1
	for sp < ep {
		if convertedStr[sp] != convertedStr[ep] {
			return false
		}
		sp++
		ep--
	}
	return true
}

func convert(s string) []byte {
	convertedStr := make([]byte, 0, len(s))
	for i := range s {
		c := s[i]
		if !((c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
			continue
		}
		if c >= 65 && c <= 90 {
			c += 32
		}
		convertedStr = append(convertedStr, c)
	}
	return convertedStr
}

📝 https://leetcode.com/problems/valid-palindrome/?envType=study-plan-v2&envId=top-interview-150

profile
두둥실

0개의 댓글